How to read and write the XML DOM with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neovantage
    New Member
    • Aug 2008
    • 245

    How to read and write the XML DOM with PHP

    Hey,
    I am working on reading and writing the XML DOM File using PHP.
    My XML File is In this Format

    [code=xml]portfolio category="Categ ory1">
    <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http ://bytes.com/images/large1_1.jpg" largeimg2="http ://bytes.com/images/large1_2.jpg" largeimg3="http ://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
    <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http ://bytes.com/images/large1_1.jpg" largeimg2="http ://bytes.com/images/large1_2.jpg" largeimg3="http ://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
    </portfolio>[/code]

    Now i am unable to read and write data in this XML Format. Can any one help me out that how to write, read, edit and delete the data from this XML file using PHP. I will be very grateful who will help me in this regard. I am badly stuck. Kindly help me please.

    Best regards,
    Mohsin Rafique
    Last edited by Atli; Dec 30 '08, 03:16 PM. Reason: Adde
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    What problem are you having?

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      option 1 – PHP: DOMDocument - Manual
      (esp. the loadXML() and save() methods, XML manipulation is done with the standard DOM functions)

      option 2 – PHP: SimpleXML - Manual
      (good for reading simple xml documents)

      option 3 – PHP: XSL - Manual
      (applying all changes in one go, XSLT knowledge (some) required)

      regards

      Comment

      • neovantage
        New Member
        • Aug 2008
        • 245

        #4
        My Problem is that how can i read and write the above formatted XML file using PHP, edit the specific existing record and delete.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          that depends on you specific task. there might be situations when it's better to do XSLT (xml structure changes) and some where a simple DOM method will do.

          as for loading, changing and saving your xml, read the manuals. they explain it pretty straightforward .

          regards

          Comment

          • neovantage
            New Member
            • Aug 2008
            • 245

            #6
            The thing is my XML is not in the format as the examples given using SimpleXML or the examples given in PHP: DOMDocument. Can you write a sample code for me that how can i read this formatted document and add a new record or edit existing record using PHP.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              have you actually tried to load your xml? (the samples are samples after all.... my own xml files are different too, they even have namespace, but they work)

              if it doesn't work, what error message do you get?

              Comment

              • neovantage
                New Member
                • Aug 2008
                • 245

                #8
                Yeh i have tried but it shows nothing, here is my code
                [code=php]
                $doc = new DOMDocument();
                $doc->load('../xml/1.xml');
                $portfolio = $doc->getElementsByT agName( "portfolio" );
                foreach( $portfolio as $row ){
                $items = $row->getElementsByT agName( "item" );
                $item = $items->item(0)->nodeValue;
                echo $item;
                }
                [/code]

                My loaded XML document is in this format
                [code=xml]
                <portfolio category="Categ ory1">
                <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http ://bytes.com/images/large1_1.jpg" largeimg2="http ://bytes.com/images/large1_2.jpg" largeimg3="http ://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
                <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http ://bytes.com/images/large1_1.jpg" largeimg2="http ://bytes.com/images/large1_2.jpg" largeimg3="http ://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
                </portfolio>
                [/code]
                Last edited by Atli; Dec 30 '08, 03:17 PM. Reason: Added [code] tags

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  ok, that's something to work with.

                  do you know if you actually enter the foreach loop? (i.e. if you have a non-empty result set)

                  why using getElementsByTa gName() on <portfolio>? that seems unnecessary to me.

                  regards

                  PS: what do you expect item(0)->nodeValue to be? you're requesting the value of the element node (which in DOM is strictly speaking non-existant) you could get the value of the text node (which would be $node->firstChild->nodeValue). I think you get the text node's value.......... ...... of an empty element!

                  try
                  Code:
                  var_dump($item);
                  Last edited by Dormilich; Dec 30 '08, 10:30 AM. Reason: after some tryouts

                  Comment

                  • neovantage
                    New Member
                    • Aug 2008
                    • 245

                    #10
                    Originally posted by Dormilich
                    ok, that's something to work with.

                    do you know if you actually enter the foreach loop? (i.e. if you have a non-empty result set)

                    why using getElementsByTa gName() on <portfolio>? that seems unnecessary to me.

                    regards
                    i am using getElementsByTa gName() on <portfolio> because i need to read the category name and as you can see the tag <item> has element title and title value but i am unable to read it. How can i read the title, type, detail, company, year, thumbimg, largeimg1, largeimg2, largeimg3 values? and how can i add a new record which append at the end of my alredy exisiting record

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      Originally posted by neovantage
                      How can i read the title, type, detail, company, year, thumbimg, largeimg1, largeimg2, largeimg3 values?
                      DOMElement::get Attribute();

                      Originally posted by neovantage
                      and how can i add a new record which append at the end of my alredy exisiting record
                      DOMDocument::cr eateElement();
                      DOMNode::append Child();

                      basicly, standard DOM procedures, only they are splitted over several DOM XML classes

                      Comment

                      • neovantage
                        New Member
                        • Aug 2008
                        • 245

                        #12
                        i am unable to use that function as i need an example script which use DOMElement::get Attribute(); because i am not that much professional to get the picture of just having the function and performed further operations. Kindly help me out i really need to know about it.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          let's make an example.

                          get the category value (tested and working):
                          Code:
                          $doc = new DOMDocument();
                          $doc->load($xml); // $xml is your xml file
                          $portfolio = $doc->getElementsByTagName( "portfolio" );
                          $item = $portfolio->item(0)->getAttribute('category');
                          // prints: Category1
                          echo $item;
                          PS: if you know how to use DOM in Javascript, this is pretty much the same

                          regards

                          Comment

                          • neovantage
                            New Member
                            • Aug 2008
                            • 245

                            #14
                            Originally posted by Dormilich
                            ok, that's something to work with.

                            do you know if you actually enter the foreach loop? (i.e. if you have a non-empty result set)

                            why using getElementsByTa gName() on <portfolio>? that seems unnecessary to me.

                            regards

                            PS: what do you expect item(0)->nodeValue to be? you're requesting the value of the element node (which in DOM is strictly speaking non-existant) you could get the value of the text node (which would be $node->firstChild->nodeValue). I think you get the text node's value.......... ...... of an empty element!

                            try
                            Code:
                            var_dump($item);
                            Thank you very very very muchhhhhhhhhhh.
                            It works now i done the same thing with the inner tag and it is working that's cool

                            My Next question is that " How can i traverse whole recodes so that i can show e.g. when we write the db query then we can fetch array by using while condition with mysql_fetch_arr ay(records) and it automatically terminates once it will be at last record. So is there any method here in XML that i can fetch all the records and it terminate automatically once the last record found.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              Originally posted by neovantage
                              So is there any method here in XML that i can fetch all the records and it terminate automatically once the last record found.
                              Code:
                              foreach (DOMNodeList $list as DOMNode $node)
                              {
                                  // code comes here
                              }
                              note that I put in the class names to make clear, what types $list & $node are, of course you call foreach as you usually do.

                              in some cases you can also use the while loop, depending on the exit definition.

                              regards

                              Comment

                              Working...