awebbdesign Posted September 29, 2011 Share Posted September 29, 2011 I have got some xml which I have ran $xml = simplexml_load_string($result); Now I need to be able to access the values in a for loop, I've done this before, but it escapes me. The XML code is as follows... <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <branches xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="http://webservices.vebra.com/export/xsd/v1/exportapi.xsd"> <branch> <name>Swindon</name> <firmid>11548</firmid> <branchid>1</branchid> <url>http://webservices.vebra.com/export/primaryexechomesAPI/v1/branch/3762</url> </branch> <branch> <name>Swindon</name> <firmid>17548</firmid> <branchid>11</branchid> <url>http://webservices.vebra.com/export/primaryexechomesAPI/v1/branch/10061</url> </branch> </branches> My code for trying to get the values when in a for loop is... # format xml and insert the different branches into the db $xml = simplexml_load_string($result); # loop through the number of branches //$branches = count($xml); for($b = 0; $b < count($xml); $b++) { //$name = $xml->name; //$name = $xml->['name']; //$name = $xml['name']; //$name = $xml['branch']; //$name = $xml->['branch']['name']; //$name = $xml[$b]->['branch']['name']; $name = $xml->['branch']['name']; print "<h1>Name</h1>"; } As you can see I've tried to get the value out, just cant seem to do it. Any help will be much appreciated! Link to comment https://forums.phpfreaks.com/topic/248119-xml-newbie/ Share on other sites More sharing options...
xyph Posted September 29, 2011 Share Posted September 29, 2011 <?php $str = '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <branches xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="http://webservices.vebra.com/export/xsd/v1/exportapi.xsd"> <branch> <name>Swindon</name> <firmid>11548</firmid> <branchid>1</branchid> <url>http://webservices.vebra.com/export/primaryexechomesAPI/v1/branch/3762</url> </branch> <branch> <name>Swindon</name> <firmid>17548</firmid> <branchid>11</branchid> <url>http://webservices.vebra.com/export/primaryexechomesAPI/v1/branch/10061</url> </branch> </branches>'; $xml = new SimpleXMLElement( $str ); if( is_object($xml->branch) ) { echo '<pre>'; foreach( $xml->branch as $branch ) { print_r( $branch ); } echo '</pre>'; } ?> Let me know if you have questions. Link to comment https://forums.phpfreaks.com/topic/248119-xml-newbie/#findComment-1274094 Share on other sites More sharing options...
awebbdesign Posted September 29, 2011 Author Share Posted September 29, 2011 It printed out the following... SimpleXMLElement Object ( [name] => Swindon [firmid] => 11548 [branchid] => 1 => http://webservices.vebra.com/export/primaryexechomesAPI/v1/branch/3762 ) Name: SimpleXMLElement Object ( [name] => Swindon [firmid] => 17548 [branchid] => 11 => http://webservices.vebra.com/export/primaryexechomesAPI/v1/branch/10061 ) Name: Im trying to get each of the object names into variables so I tried... if( is_object($xml->branch) ) { echo '<pre>'; foreach( $xml->branch as $branch ) { print_r( $branch ); $name = $branch['name']; print "<p>Name: $name</p>"; } echo '</pre>'; } It didn't assign the name Swindon to $name! Link to comment https://forums.phpfreaks.com/topic/248119-xml-newbie/#findComment-1274110 Share on other sites More sharing options...
xyph Posted September 30, 2011 Share Posted September 30, 2011 $name = $branch->name; Link to comment https://forums.phpfreaks.com/topic/248119-xml-newbie/#findComment-1274293 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.