You're nearly there. The SimpleXMLElement
class doesn't really offer any methods to access a specific attribute, or get at its value. What it does do is implement the Taversable
interface, and it supports array access. The class documentation is well worth a look, especially the user contributions under SimpleXMLElement::attributes
, which actually tell you how you can echo the ID's you're after.
Basically, you can keep everything you have so far (including the $hr = $xml->xpath();
). After that, it's a simple matter of iterating over the SimpleXMLElement
instances in $hr
, and doing this:
foreach ($hr as $set) {//set is a SimpleMLElement instance
echo 'ID is: ', (string) $set['id'];
}
As you can see, the attributes are accessible as array indexes, and are SimpleXMLElements
, too (so you need to cast them to a string to generate the output you want).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…