inserting node in XMl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scn87
    New Member
    • Oct 2008
    • 7

    #1

    inserting node in XMl

    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........Thank s for any help....
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if you have a DOM implementation use the parentNode.appe ndChild() method.

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

    regards

    Comment

    • scn87
      New Member
      • Oct 2008
      • 7

      #3
      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

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

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

        regards

        Comment

        • scn87
          New Member
          • Oct 2008
          • 7

          #5
          Code:
          <?php 
           $i=0;
           $sum=0;
           $dom = new DomDocument('1.0'); 
           $books = $dom->appendChild($dom->createElement('books')); 
           $qry = "select Drg_id,SUM(Consumption) from id_copy Group By Drg_id Order By SUM(Consumption) DESC";
           $res = QrySelect($qry);
          while($arr = mysql_fetch_array($res))
          {
          	 $Drugs = $books->appendChild($dom->createElement('Drugs')); 
          	 $DRGID = $Drugs->appendChild($dom->createElement('DRGID')); 
          	 $DRGID->appendChild($dom->createTextNode($arr[0])); 
          	 $Consumption = $Drugs->appendChild($dom->createElement('Consumption')); 
          	 $Consumption->appendChild($dom->createTextNode($arr[1])); 
               $sum+=$arr[1];
          
          	}
          $s = simplexml_import_dom($dom);
          
          foreach ($s->Drugs as $Drgs)
          {
               $cont = ($Drgs->Consumption/$sum)*100;
          	 $cumultn=($cont)+($cumultn);
          	 
          
          	 $Contribution = $Drugs->appendChild($dom->createElement('Contribution')); 
          	 $Contribution->appendChild($dom->createTextNode($cont)); 
          	 $Drugs->appendChild($Contribution);
          	
          	 $Cumulation= $Drugs->appendChild($dom->createElement('Cumulation')); 
          	 $Cumulation->appendChild($dom->createTextNode($cumultn)); 
          	 $Drugs->appendChild($Cumulation);
          }
          
           $dom->formatOutput = true; 
           $test1 = $dom->saveXML(); 
           $dom->save('test1.xml'); 
           
           ?>
          Contribution and cumulation must be attached to drugs.......... .......thats what i really need........... ..thanks !!!!!
          Last edited by Markus; Nov 13 '08, 09:49 AM. Reason: added # tags

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            please use [code] tags when posting code!

            btw. which node is making problems?

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              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.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                from what I can see, there is no SimpleXMLElemen t->appendChild( ) function, it's named SimpleXMLElemen t->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

                Comment

                • scn87
                  New Member
                  • Oct 2008
                  • 7

                  #9
                  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.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    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

                    Comment

                    Working...