-1

I am trying to use simple XML to display the results from an XML feed in PHP.

Here is my PHP

$url = 'http://somefeed.xml';
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);


foreach($xml->entry as $x) {
    echo "Name".$x->title."<br>";

}

Here is an out take of the xml file I am trying to display

<entry>
        <id>test ed</id>
        <title type="text">My House</title>
        <updated>2016-03-31T22:45:06Z</updated>
        <content type="application/xml">
            <s:organisationSummary>
                <s:name>my house</s:name>
                <s:odsCode>61bv</s:odsCode>
                <s:address>
                    <s:addressLine>House Name</s:addressLine>
                    <s:addressLine>House Road</s:addressLine>
                    <s:addressLine>House Town</s:addressLine>
                    <s:postcode>House Postode</s:postcode>
                </s:address>
            </s:organisationSummary>
        </content>
    </entry>

With the PHP I have written, it displays the title perfectly, but I am at a loss with how to display the s:organisationSummary details. I would like them to be able to echo the s:name and each of the s:addressLine tags individually.

Any help would be appreciated.

Will Banfield
  • 21
  • 1
  • 4

1 Answers1

1

The part before colon is called namespace prefix. One way to access element in namespace is using xpath() method, for example :

foreach($xml->entry as $x) {
    echo $x->xpath("content/s:organisationSummary/s:name")[0]."<br>";
}

Related : Parse XML with Namespace using SimpleXML

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137