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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.