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 6341543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:03:17+00:00 2026-05-24T20:03:17+00:00

I have this PHP code that adds a set of nodes to an XML

  • 0

I have this PHP code that adds a set of nodes to an XML file. It does most of what it should do, it finds where to make the additions, but then it doesn’t make the update.

I have an xml file of this structure.

<COMMUNITIES>
  <COMMUNITY ID="c001">
       <URLS>
           <URL ID="u001">
              <NAME>Google.com</NAME>
              <URL>http://www.google.com</URL>
           </URL>
       </URLS>
  </COMMUNITY>
</COMMUNITIES>

The updates are being made to the URLS node. The user clicks an “Add URL” link and I add the new URL to this node like this.

       <URL ID="u002">
          <NAME>Yahoo.com</NAME>
          <URL>http://www.yahoo.com</URL>
       </URL>

If the URLS node exists, I append the new url node. If the URLS node does not exist, I create it, then append the new url node.

There are no errors, everything seems to work, but the URLS node is empty. Here is the PHP script.

function add_url( $nodeid, $urlid, $urlname, $urllink ) {

$dom = new DOMDocument();
$dom->load('communities.xml');

$dom->formatOutput = true; 
$dom->preserveWhiteSpace = false;

// get document element  

$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//COMMUNITY[@ID='$nodeid']"); 

if ($nodes->length) {
   $node = $nodes->item(0); 

   $xurl = $dom->createElement("URL"); 
   $xurl->setAttribute("ID", $urlid);

   $xuname     = $dom->createElement("NAME");  
   $xunameText = $dom->createTextNode(mysql_escape_mimic($urlname));  
   $xuname->appendChild($xunameText); 

   $xulink     = $dom->createElement("URL");  
   $xulinkText = $dom->createTextNode(mysql_escape_mimic($urllink));  
   $xulink->appendChild($xulinkText); 

   $xurl->appendChild($xuname); 
   $xurl->appendChild($xulink); 

   $xurls = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS");

   if ($xurls->length) {
   }
   else {
      $xurls = $dom->createElement("URLS"); 
   }

   $xurls->appendChild($xurl);

   /* $node->appendChild($xurls); */

}

$dom->save('communities.xml');
}

I thought the problem was that I was appending the URLS node when it already existed. But I commented out that line. I am sure the problem is simple, but I am new at this. I looked on the site and did not find that specific situation.

Edit

  1. Removed return statement before if ($xurls->length).

  2. Fixed function.

  • 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-05-24T20:03:19+00:00Added an answer on May 24, 2026 at 8:03 pm

    The problem was that I was attempting to add nodes to a node which was already in the XML file as empty.

    Instead of the node having data like this.

    <URLS>
       <URL ID="u001">
          <NAME>Google.com</NAME>
       </URL>
    </URLS>
    

    I had a node like this.

    <URLS/>
    

    Common sense says that this should not be a problem. If the node is empty then appendChild should just add the data and rewrite the node as.

    <URLS>
       <URL ID="u002">
          <NAME>Yahoo.com</NAME>
       </URL>
    </URLS>
    

    Here is the PHP script that worked. You will note that I created a new node instead of appending to <URLS/>. I also reordered the appendChild to start from the top node instead of the creating the child nodes first then appending a set of nodes to the top node.

    function add_url( $nodeid, $urlid, $urlname, $urllink ) {
    
    $dom = new DOMDocument();
    $dom->load('communities.xml');
    
    $dom->formatOutput = true; 
    $dom->preserveWhiteSpace = true;
    
    // get document element  
    
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query("//COMMUNITY[@ID='$nodeid']"); 
    
    if ($nodes->length) {
    
       $node = $nodes->item(0); 
    
       $xurls = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS");
    
    
       if ($xurls->length) {
       }
       else {
          $xurls = $dom->createElement("URLS"); 
          $node->appendChild($xurls);
       }
    
          $xurls = $dom->createElement("URLSC"); 
          $node->appendChild($xurls);
    
       $xurl = $dom->createElement("URL"); 
       $xurl->setAttribute("ID", $urlid);
       $xurls->appendChild($xurl);
    
       $xuname     = $dom->createElement("NAME");  
       $xunameText = $dom->createTextNode(mysql_escape_mimic($urlname));  
       $xuname->appendChild($xunameText); 
       $xurl->appendChild($xuname); 
    
       $xulink     = $dom->createElement("URLC");  
       $xulinkText = $dom->createTextNode(mysql_escape_mimic($urllink));  
       $xulink->appendChild($xulinkText); 
       $xurl->appendChild($xulink); 
    
    }
    
    $dom->save('communities.xml');
    }
    

    So I must modify the function that creates the empty node <URLS/>. I should not create the node in that function but create it here in the add_url function only when it doesn’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm makin' a scripting language interpreter using PHP. I have this code in that
In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
I have this php code, with which I am trying to generate a popup
I have this PHP code echo '<a href=# onclick=updateByQuery(\'Layer3\', ' . json_encode($query) . ');>Link
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
Say i have this PHP code: $FooBar = a string; i then need a
I found this PHP code in an app I have to modify... $links =
I have this code in PHP. It connects to the DB fine, but pops
This is my php code (I already have a connection to the db): $result
I have this code in userpage.php: <script langauge=JavaScript><!-- function newWindow(fileName,windowName) { msgWindow=window.open(fileName,windowName); } //--></script>

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.