【问题标题】:get @attributes out of array from simplexml_load_file after xpath with php使用 php 在 xpath 之后从 simplexml_load_file 中获取 @attributes
【发布时间】:2018-12-26 14:33:54
【问题描述】:

我有一个 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
                            )

                    )

            )

    )

如何访问“路径”等值?我有问题 [0] => SimpleXMLElement Object 那么节点的名称是什么?

echo (string)$result2->0[0]->attributes()->path;

没用.....

谢谢

【问题讨论】:

    标签: php xml xpath attributes simplexml


    【解决方案1】:

    虽然检查 XML 会更容易,但您可能会发现正确的表示法是...

    echo (string)$result2[0]->attributes()->path;
    

    这假设您想要 XPath 找到的第一个项目。

    你也可以找到

    echo (string)$result2[0]['path'];
    

    有效(但并非总是如此)。

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 2012-07-08
      • 1970-01-01
      相关资源
      最近更新 更多