php simplexml issue in reading an attribute that h

2019-03-04 03:21发布

问题:

I have been trying to read an xml attribute that has a " : " in it, but I'm having trouble...specifically "yweather:condition"

This is my code:

 if ($xml = simplexml_load_file("http://weather.yahooapis.com/forecastrss?p=LEXX0003&u=c")) {
    $namespacesMeta = $xml->getNamespaces(true);
    $yweather = $xml->children($namespacesMeta['yweather']);
    $docMeta = $yweather->{'condition'};
    var_dump($docMeta);
}

i got here after reading off another thread on stackoverflow, but the result is not as I expected, I get the following:

object(SimpleXMLElement)[3]

You can check the above link to see the full xml,

I want to read the attributes in "yweather:condition" I know how to access and read the other parts of the XML, but this one is being tricky...I also tried getAttributes() and it did not work

thanks

回答1:

$docMetaAttributes = $docMeta->attributes(); 

or

$docMetaAttributes = $docMeta->attributes($namespacesMeta['yweather']);  

for namespaced attributes in the yweather namespace

http://www.php.net/manual/en/simplexmlelement.attributes.php