Connecting Tech Pros Worldwide Forums | Help | Site Map

inserting node in XMl

Newbie
 
Join Date: Oct 2008
Posts: 7
#1: Nov 13 '08
hi..........

i am new to this.....i am doing a XML based application..i have two nodes and after doing certain calculations i need to add the third node.....the third node is created but it is not getting attached to parent node...it comes in the last section of program...what should i do........Thanks for any help....

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,656
#2: Nov 13 '08

re: inserting node in XMl


if you have a DOM implementation use the parentNode.appendChild() method.

in case your application is written in PHP make use of the DOMDocument class

regards
Newbie
 
Join Date: Oct 2008
Posts: 7
#3: Nov 13 '08

re: inserting node in XMl


hi...........

thanks.............

i have used DomDocument and also appendChild method.but still its working in the same old way........

the third node is created but not attached to parent node
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,656
#4: Nov 13 '08

re: inserting node in XMl


maybe I can see anything if you post the code (using [code] tags).

regards
Newbie
 
Join Date: Oct 2008
Posts: 7
#5: Nov 13 '08

re: inserting node in XMl


Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  $i=0;
  3.  $sum=0;
  4.  $dom = new DomDocument('1.0'); 
  5.  $books = $dom->appendChild($dom->createElement('books')); 
  6.  $qry = "select Drg_id,SUM(Consumption) from id_copy Group By Drg_id Order By SUM(Consumption) DESC";
  7.  $res = QrySelect($qry);
  8. while($arr = mysql_fetch_array($res))
  9. {
  10.      $Drugs = $books->appendChild($dom->createElement('Drugs')); 
  11.      $DRGID = $Drugs->appendChild($dom->createElement('DRGID')); 
  12.      $DRGID->appendChild($dom->createTextNode($arr[0])); 
  13.      $Consumption = $Drugs->appendChild($dom->createElement('Consumption')); 
  14.      $Consumption->appendChild($dom->createTextNode($arr[1])); 
  15.      $sum+=$arr[1];
  16.  
  17.     }
  18. $s = simplexml_import_dom($dom);
  19.  
  20. foreach ($s->Drugs as $Drgs)
  21. {
  22.      $cont = ($Drgs->Consumption/$sum)*100;
  23.      $cumultn=($cont)+($cumultn);
  24.  
  25.  
  26.      $Contribution = $Drugs->appendChild($dom->createElement('Contribution')); 
  27.      $Contribution->appendChild($dom->createTextNode($cont)); 
  28.      $Drugs->appendChild($Contribution);
  29.  
  30.      $Cumulation= $Drugs->appendChild($dom->createElement('Cumulation')); 
  31.      $Cumulation->appendChild($dom->createTextNode($cumultn)); 
  32.      $Drugs->appendChild($Cumulation);
  33. }
  34.  
  35.  $dom->formatOutput = true; 
  36.  $test1 = $dom->saveXML(); 
  37.  $dom->save('test1.xml'); 
  38.  
  39.  ?>
  40.  
Contribution and cumulation must be attached to drugs.................thats what i really need.............thanks !!!!!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,656
#6: Nov 13 '08

re: inserting node in XMl


please use [code] tags when posting code!

btw. which node is making problems?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#7: Nov 13 '08

re: inserting node in XMl


scn87, when you post code, for our experts sake, it makes it a whole lot easier to read if you wrap it in [code] tags. If you're unsure how to do that, highlight the code you have in the textarea and then select the '#' key above the textarea.

Please do pay attention to this as it is one of our Posting Guidelines.

Moderator.

ps. Why do you use so many periods (full stops)? It's unnecessary and makes your post difficult to read.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,656
#8: Nov 13 '08

re: inserting node in XMl


from what I can see, there is no SimpleXMLElement->appendChild() function, it's named SimpleXMLElement->addChild(). refer to the SimpleXML reference for more information.

just out of interest, isn't it possible to do the task in either DOMDocument or SimpleXML (to avoid confusion)?

regards
Newbie
 
Join Date: Oct 2008
Posts: 7
#9: Nov 13 '08

re: inserting node in XMl


how to do the task in either DOMDocument or SimpleXML?Can you please help me with an example?
And problem is with 3rd and 4th node.That is contribution node and cumulation node.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,656
#10: Nov 13 '08

re: inserting node in XMl


well, you create and append Nodes in both parts of your script, so you basicly use the same language constructs on two implementations. since I do not use either of them (I normally do XSLT) I can't provide you with a working example, nevertheless the references of SimpleXML and DOMDocument (links above) should contain the examples you might need.

regarding the 3rd and 4th node, I already gave you the answer to that problem.

regards
Reply