homepage Welcome to WebmasterWorld Guest from 54.161.166.171
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
My XML Problem
cyril kearney




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

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

 

httpwebwitch




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

try

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

or

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

documentation [php.net]

cyril kearney




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

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";
}

httpwebwitch




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

wicked.
Always glad to help.
~ hww

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved