Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: httpwebwitch

Message Too Old, No Replies

My XML Problem

   
12:01 am on Nov 30, 2009 (gmt 0)

10+ Year Member



<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<!DOCTYPE shrs (View Source for full doctype...)>
- <shrs>
- <rs>
- <r>
<src url="http://www.example.com">www.example.com</src>
<e>- Description 1</e>
</r>
- <r>
<src url="http://www.example.com">www.example.com</src>
<e>- Description 2</e>
</r>
- <r>
<src url="http://www.example.com">www.example.com</src>
<e>- Description 3</e>
</r>
</rs>
</shrs>

I have an XML problem. I have attached a simplified XML file.

I can display 'src' which would be www.cnn.com, www.yahoo.com and www.google.com and the associated 'e' which would be Description 1, Description 2, and Description 3.

I cannot display 'src url' which should be http://www.example.com, http://www.example.com, and http://www.example.com.

I don't know how to address this element properly. I read the file using curl and got the XML in an object and can see it in the echo of $oXML.

In my example I can display both 'src' and 'e' but not 'url'.

Any help would be appreciated!

<?php
//. . . fragment note download_page reads the file in curl

$sXML = download_page('http://www.example.com');
$oXML = new SimpleXMLElement($sXML);

echo $oXML;

foreach($oXML->rs as $oEntry){
for ( $idx = 0; $idx < 3; $idx += 1)
{
$src = $oEntry->r[$idx]->src;
echo $src;
$e = $oEntry->r[$idx]->e;
echo $e;
$url = $oEntry->r[$idx]->src->url;
echo $url;

}
}
// end of fragment
?>

[edited by: httpwebwitch at 8:49 am (utc) on Dec. 27, 2009]
[edit reason] examplified [/edit]

4:08 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



try

$oEntry->r[$idx]->src['@attributes']['url']

or

$oEntry->r[$idx]->src->attributes()['url']

documentation [php.net]

6:20 pm on Nov 30, 2009 (gmt 0)

10+ Year Member



Thank you httpwebwitch for editing my original message, solving my problem and pointing me to the specific documentation.

// original that didn't work
$url = $oEntry->r[$idx]->src->url;
echo $url;

// httpwebwitch code modified to work in my example
foreach($oEntry->r[$idx]->src->attributes() as $a => $url) {
echo $url,"\"\n";
}

6:53 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



wicked.
Always glad to help.
~ hww
 

Featured Threads

Hot Threads This Week

Hot Threads This Month