0

Possible Duplicate:
PHP simpleXML how to save the file in a formatted way?

Is it possible to use both SimpleXML and DOM Document together?

I'm using SimpleXML to get a file update it and save it but the xml is formatted on one long line.

How can I use DOM Document in the script to format the output? Does it have to be done after the SimpleXML bit hs finished writing to the file, or can it be done before?

Thanks

Community
  • 1
  • 1
Ben Iskander
  • 606
  • 10
  • 22

1 Answers1

0
//Get the wordpress postID
$postID = get_the_ID();

$postData = get_post($postID);

// echo $postID.'<br />'.$postData->post_title.'<br />'.$postData->post_date_gmt.'<br />';

$xmlFile = '/Applications/MAMP/htdocs/giraffetest/test.xml';

// load the document
$xml = simplexml_load_file($xmlFile);

// Check to see if the post id is already in the xml file - has it already been set?
$nodeExists = $xml->xpath("//post[@id=".$postID."]");

//Count the results
$countNodeExists = count($nodeExists);

if($countNodeExists > 0) { // If the ID is already in the file

        // echo 'ID already here';

        // get the correct node
        $result = $xml->xpath("//post[@id=".$postID."]/postviews");

        // heres the trick - the first result of xpath, and the node value (stored in [0])
        $result[0][0] = $result[0][0]+1;

} else { // If the ID isn;'t there, add a new entry in the xml file for the post

        //echo 'ID added';

        $postNode = $xml->addChild('post'); // adding a new <post> to the top level node
    $postNode->addAttribute('id', $postID); // adding a <postid> inside the new <post>
    $postNode->addChild('postviews', 1); // adding a postviews inside the new <post>
}

// save the updated document

//$xml->asXML($xmlFile);
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save($xmlFile);
Sergey Karasev
  • 4,513
  • 4
  • 25
  • 24
  • look [DOMDocument class](http://php.net/manual/en/class.domdocument.php) – Sergey Karasev Nov 22 '12 at 12:12
  • Hi ahiipsa, thanks for that. Where does the code go though? – Ben Iskander Nov 22 '12 at 12:17
  • 1
    Here is my code I have written so far. Where in this file would your code need to go? - http://pastebin.com/QzRfBAeQ – Ben Iskander Nov 22 '12 at 12:34
  • update a answer (If I understand you correctly) – Sergey Karasev Nov 22 '12 at 12:40
  • There is an extra ; in the loadXML line. I've removed it, but I'm getting the following error… Catchable fatal error: Argument 1 passed to DOMDocument::saveXML() must be an instance of DOMNode, string given, called in /Applications/MAMP/htdocs/giraffetest/wp-includes/theme.php on line 1117 and defined in /Applications/MAMP/htdocs/giraffetest/wp-content/themes/canvas-child/content-post.php on line 47 – Ben Iskander Nov 22 '12 at 12:44
  • sorry, made a mistake, correct saveXML () -> save () update answer to http://pastebin.com/vsULvGMu – Sergey Karasev Nov 22 '12 at 12:46
  • Brilliant! Thanks very much for your help :-) There's still the extra ; in the `loadXML` line. Should be `$dom->loadXML($xml->asXML());` instead of `$dom->loadXML($xml->asXML(););` – Ben Iskander Nov 22 '12 at 12:55