Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8885243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:15:40+00:00 2026-06-14T21:15:40+00:00

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

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:15:41+00:00Added an answer on June 14, 2026 at 9:15 pm
    //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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP get both array value and array key I am using Codeigniters'
Possible Duplicate: php - efficent way to get and remove first line in file
Possible Duplicate: Parse XML with Namespace using SimpleXML PHP SimpleXML Namespace Problem <?php header(Content-Type:
Possible Duplicate: Getting content using wikipedia API Using PHP, how do I get the
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: PHP Get end string on url between / and / I have
Possible Duplicate: php mail() function on localhost I am using php mail(). I have
Possible Duplicate: PHP class instantiation. To use or not to use the parenthesis? Omission
Possible Duplicate: PHP method chaining? I occasionally see some php applications use classes like
Possible Duplicate: foreach and simplexml I have got my XML document loading correct but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.