Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SimpleXML获得不同的属性_Php_Xml_Simplexml - Fatal编程技术网

Php SimpleXML获得不同的属性

Php SimpleXML获得不同的属性,php,xml,simplexml,Php,Xml,Simplexml,可能重复: 如何从这个XML提要中获取特定属性 示例-我一直在使用类似于此的行来获取其他XML详细信息,但我不确定如何更改它以获取特定属性 $mainpropertydeails=$mainPropertyUrl->Attributes->attribute 属性: <Attributes> <Attribute> <Name>bedrooms</Name> <DisplayName>Bedrooms</DisplayName&

可能重复:

如何从这个XML提要中获取特定属性

示例-我一直在使用类似于此的行来获取其他XML详细信息,但我不确定如何更改它以获取特定属性

$mainpropertydeails=$mainPropertyUrl->Attributes->attribute

属性:

<Attributes>
<Attribute>
<Name>bedrooms</Name>
<DisplayName>Bedrooms</DisplayName>
<Value>4 bedrooms</Value>
</Attribute>
<Attribute>
<Name>bathrooms</Name>
<DisplayName>Bathrooms</DisplayName>
<Value>2 bathrooms</Value>
</Attribute>
<Attribute>
<Name>property_type</Name>
<DisplayName>Property type</DisplayName>
<Value>House</Value>
</Attribute>

卧室
卧室
4间卧室
浴室
浴室
2间浴室
属性类型
属性类型
房子

SimpleXML
将这些节点实现为一个数组。如果您要
var\u dump()
this,您将看到如下内容:

// 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"
}
根据子节点的值获取属性节点: 使用,您可以根据子节点的文本值查询父节点<代码>属性节点:

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

使用
$sxe=newsimplexmlement($xml);变量转储($sxe->Attribute[0])
其中
0
是所需属性的特定ID