XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 17th, 2011, 08:18 PM
mmmosias mmmosias is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 18 mmmosias User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 55 sec
Reputation Power: 0
Adding nodes to XML

I am adding nodes to an XML file. Here is the structure.

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


I want to update the URLS node to add a new URL.

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


Here is the PHP script.

Code:
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); 

   $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);

}

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


The function runs through. The variables coming in are OK. The calls to appendChild don't cause errors. So I am wondering what might be the problem.

Reply With Quote
  #2  
Old August 19th, 2011, 07:20 PM
mmmosias mmmosias is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 18 mmmosias User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 55 sec
Reputation Power: 0
The problem was that I was adding nodes to a short tagged node, e.g. <URLS/>. This "empty" node was created by a function ealier. If I create this node in add_url() just before adding the nodes it contains, then the new nodes are added and everything works.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Adding nodes to XML

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap