0

i want to load XML data into my php file with address data. Each address should also have coordinates - if not they should be added. So i am doing the following:

        $xmlDatei = "AddressData.xml";
        $xml = simplexml_load_file($xmlDatei);

        for($i=0,$size=count($xml);$i<$size;$i++){

            if($xml->RECORD[$i]->ADDRESS->LAT != NULL){
                //get lat and lng stuff here...

                $lat = .......
                $lng = .......


                echo "lat: " . $lat; // Test echo WORKING
                echo "lng: " . $lng;

                // Now i want to add the data to the xml
                $xml->RECORD[$i]->ADDRESS->addAttribute('LAT', $lat);
                $xml->RECORD[$i]->ADDRESS->addAttribute('LNG', $lng);

                $xml->saveXML();
            }

            // Test echo NOT WORKING
            echo $xml->RECORD[$i]->ADDRESS->LAT;
            echo $xml->RECORD[$i]->ADDRESS->LNG;
        }

So it seems like the addAttribute is not working properly here.

What am I doing wrong???

Tim Döring
  • 41
  • 1
  • 8

2 Answers2

0

Your echo is looking for a child element called LAT:

echo $xml->RECORD[$i]->ADDRESS->LAT;

But you have added an attribute, so you need to use different syntax:

echo $xml->RECORD[$i]->ADDRESS['LAT'];
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • thanks for your quick response this is working for my echo now. but its still not appearing in my xml document??? – Tim Döring Aug 22 '14 at 11:16
  • @TimDöring Do you mean the output of `$xml->saveXML();`? Note that that doesn't save the output to a file, it's [just an alias of `->asXML()`](http://php.net/manual/en/simplexmlelement.savexml.php). – IMSoP Aug 22 '14 at 11:20
0

You are adding attributes to the ADDRESS tag, not nodes.

Try this:

echo $xml->RECORD[$i]->ADDRESS['LAT'];
echo $xml->RECORD[$i]->ADDRESS['LNG'];
web-nomad
  • 6,003
  • 3
  • 34
  • 49
  • Pass the filename by which you want to save the xml file in `saveXML()`. `$xml->saveXML(YOUR_XML_FILE_NAME);` – web-nomad Aug 22 '14 at 11:26