I have a XML file and I want to read this and add each node to a variable... ie

<Table>1<Table
<Person>John</Person>

to

$Table
$Person

So that I can do stuff with the data...

    If you want to do more with the data, beside for creating (I can't seem to remember if you can delete nodes with simpleXML, for some reason my mind is saying no, but her sexy soft body is screaming yes) and changing node values, you'd be better of skipping directly to DOM.

    For PHP 5: http://se2.php.net/manual/en/ref.dom.php

    Which does not apply for PHP <5.

      Neither of these links teach me how to open an XML file and get all the node names and get their values into a variable so I can add it to a DB. Email some and compare figures etc....

      HOw can I learn this/.

        Neither of these links teach me how to open an XML file and get all the node names and get their values into a variable so I can add it to a DB. Email some and compare figures etc....

        HOw can I learn this/.

        This is literally copy/pasted from the link above.

        DOMDocument->load() - Load XML from a file

        That will load the xml contents from a file into an object. From there you can loop through the object (recursively, depending on how many levels you have) and save to your database.

          Yeah so I have

          $doc = new DOMDocument();
          $doc->load('tables.xml');
          echo $doc->saveXML();
          

          and this echo shows this

          2 Melbourne Storm 22 19 3 0 1 539 265 274 40
          2 Cronulla Sharks 22 8 14 0 1 413 379 34 18
          2 Sydney Roosters 22 9 12 1 1 397 570 -173 21
          2 Canberra Raiders 22 9 13 0 1 491 600 -109 20
          2 South Sydney Rabbitohs 22 11 11 0 1 359 361 -2 24

          as expected... but how do I loop through every node?

            And surprisingly enough, [man]simplexml_load_file/man will do the same sort of thing, creating a simplexml object you can loop through.

              Okay great I have two ways of creating a XML object but I still have no idea on how to loop through it

                This is what I have...

                foreach ($doc->documentElement->childNodes as $Tables) {
                   if ($Tables->nodeType == 1 && $Tables->nodeName == "Table") {
                   foreach ($Tables->childNodes  as $Table) {
                
                        if ($Table->nodeType == 1 && $Table->nodeName == "TeamName") {
                        $TeamName = $Table->textContent . " ";
                        }
                        if ($Table->nodeType == 1 && $Table->nodeName == "TeamP") {
                        $CompID = $Table->textContent . " ";
                        }
                
                   echo "$TeamName = $TeamP<br>";    
                } } }

                This prints the TeamName and TeamP for each Table node where this...

                foreach ($doc->documentElement->childNodes as $Tables) {
                   if ($Tables->nodeType == 1 && $Tables->nodeName == "Table") {
                   foreach ($Tables->childNodes  as $Table) {
                
                        if ($Table->nodeType == 1 && $Table->nodeName == "TeamName") {
                        $TeamName = $Table->textContent . " ";
                        echo $TeamName;
                        echo "<br>";
                        }
                        if ($Table->nodeType == 1 && $Table->nodeName == "TeamP") {
                        $CompID = $Table->textContent . " ";
                        }
                
                   }
                }
                } 
                

                Prints the each TeamName only once (which is what I want)... why is that?

                  Write a Reply...