SimpleXML
将这些节点实现为数组。如果你var_dump()
这样做,你会看到类似的东西:
// Dump the whole Attributes array
php > var_dump($xml->Attributes);
object(SimpleXMLElement)#6 (1) {
["Attribute"]=>
array(3) {
[0]=>
object(SimpleXMLElement)#2 (3) {
["Name"]=>
string(8) "bedrooms"
["DisplayName"]=>
string(8) "Bedrooms"
["Value"]=>
string(10) "4 bedrooms"
}
[1]=>
object(SimpleXMLElement)#5 (3) {
["Name"]=>
string(9) "bathrooms"
["DisplayName"]=>
string(9) "Bathrooms"
["Value"]=>
string(11) "2 bathrooms"
}
[2]=>
object(SimpleXMLElement)#3 (3) {
["Name"]=>
string(13) "property_type"
["DisplayName"]=>
string(13) "Property type"
["Value"]=>
string(5) "House"
}
}
}
因此,只需通过数组索引访问特定的问题:
// Get the second Attribute node
var_dump($xml->Attributes[0]->Attribute[1]);
object(SimpleXMLElement)#6 (3) {
["Name"]=>
string(9) "bathrooms"
["DisplayName"]=>
string(9) "Bathrooms"
["Value"]=>
string(11) "2 bathrooms"
}
根据孩子的值获取属性节点:
使用您可以根据孩子的文本值xpath()
查询父节点:Attribute
// Get the Attribute containing the Bathrooms DisplayName
// Child's text value is queried via [AttrName/text()="value"]
var_dump($xml->xpath('//Attributes/Attribute[DisplayName/text()="Bathrooms"]');
array(1) {
[0]=>
object(SimpleXMLElement)#6 (3) {
["Name"]=>
string(9) "bathrooms"
["DisplayName"]=>
string(9) "Bathrooms"
["Value"]=>
string(11) "2 bathrooms"
}
}