Jump to content

XML Newbie


awebbdesign

Recommended Posts

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.