使用 php 在 xpath 之后从 simplexml_load_file 中获取数组中的 @attributes
get @attributes out of array from simplexml_load_file after xpath with php
我有一个 xml,在使用 simplexml_load_file.
加载 xml 后,我可以使用 xpath 获取包含我需要的数据的数组
我这样试过:Access @attributes data in SimpleXMLElement in PHP
用我的 XML 阵列我仍然无法访问节点,有人可以检查我的代码:谢谢
$result2 = $xml->xpath("//file[@Catid='151']");
这是给这个数组:
数组
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[path] => export/freexml.int/DE/4757.xml
[Product_ID] => 4757
[Updated] => 20170902053143
[Quality] => ICECAT
[Supplier_id] => 3
[Prod_ID] => TT34MUK
[Catid] => 151
[On_Market] => 1
[Model_Name] => THINKPAD T23 P3-1.13G
[Product_View] => 12655
[HighPic] => http://images.icecat.biz/img/norm/high/4757-6880.jpg
[HighPicSize] => 4138
[HighPicWidth] => 200
[HighPicHeight] => 150
[Date_Added] => 20050715000000
[Limited] => No
)
[EAN_UPCS] => SimpleXMLElement Object
(
[EAN_UPC] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Value] => 3606503209062
[IsApproved] => 0
)
)
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Value] => LU
)
)
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[path] => export/freexml.int/DE/41895.xml
[Product_ID] => 41895
[Updated] => 20170902052843
[Quality] => ICECAT
[Supplier_id] => 7
[Prod_ID] => LX.T2606.067
[Catid] => 151
[On_Market] => 1
[Model_Name] => TRAVELMATE 432LC P4-2.53G
[Product_View] => 12056
[HighPic] => http://images.icecat.biz/img/norm/high/41895-65.jpg
[HighPicSize] => 14404
[HighPicWidth] => 330
[HighPicHeight] => 290
[Date_Added] => 20050715000000
[Limited] => No
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Value] => DE
)
)
)
)
如何访问 'path' 等值?我有问题
[0] => 简单XML元素对象那么节点的名称是什么?
echo (string)$result2->0[0]->attributes()->path;
没用.....
谢谢
尽管使用 XML 进行检查会更容易,但您可能会发现正确的表示法是...
echo (string)$result2[0]->attributes()->path;
这假定您需要 XPath 找到的第一个项目。
您可能还会发现
echo (string)$result2[0]['path'];
有效(但并非总是如此)。
我有一个 xml,在使用 simplexml_load_file.
加载 xml 后,我可以使用 xpath 获取包含我需要的数据的数组我这样试过:Access @attributes data in SimpleXMLElement in PHP
用我的 XML 阵列我仍然无法访问节点,有人可以检查我的代码:谢谢
$result2 = $xml->xpath("//file[@Catid='151']");
这是给这个数组: 数组
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[path] => export/freexml.int/DE/4757.xml
[Product_ID] => 4757
[Updated] => 20170902053143
[Quality] => ICECAT
[Supplier_id] => 3
[Prod_ID] => TT34MUK
[Catid] => 151
[On_Market] => 1
[Model_Name] => THINKPAD T23 P3-1.13G
[Product_View] => 12655
[HighPic] => http://images.icecat.biz/img/norm/high/4757-6880.jpg
[HighPicSize] => 4138
[HighPicWidth] => 200
[HighPicHeight] => 150
[Date_Added] => 20050715000000
[Limited] => No
)
[EAN_UPCS] => SimpleXMLElement Object
(
[EAN_UPC] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Value] => 3606503209062
[IsApproved] => 0
)
)
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Value] => LU
)
)
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[path] => export/freexml.int/DE/41895.xml
[Product_ID] => 41895
[Updated] => 20170902052843
[Quality] => ICECAT
[Supplier_id] => 7
[Prod_ID] => LX.T2606.067
[Catid] => 151
[On_Market] => 1
[Model_Name] => TRAVELMATE 432LC P4-2.53G
[Product_View] => 12056
[HighPic] => http://images.icecat.biz/img/norm/high/41895-65.jpg
[HighPicSize] => 14404
[HighPicWidth] => 330
[HighPicHeight] => 290
[Date_Added] => 20050715000000
[Limited] => No
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Value] => DE
)
)
)
)
如何访问 'path' 等值?我有问题 [0] => 简单XML元素对象那么节点的名称是什么?
echo (string)$result2->0[0]->attributes()->path;
没用.....
谢谢
尽管使用 XML 进行检查会更容易,但您可能会发现正确的表示法是...
echo (string)$result2[0]->attributes()->path;
这假定您需要 XPath 找到的第一个项目。
您可能还会发现
echo (string)$result2[0]['path'];
有效(但并非总是如此)。