猿问

php中xml的每个节点属性值

我正在尝试获取 catId 值。但我只能看到类别值。


我的 xml 文件如下所示:


<sample>

    <Item ItemNumber="00000088" FormattedItemNumber="00000-088">

      <CompatibleModels />

        <Category CatId="160" >  test 123 </Category>

      <Images />

      <Documents />

      <RequiredItems />

    </Item>

</sample>


$xml = simplexml_load_file("test.xml");

print_r($xml);


[sample] => Array

        (

            [0] => SimpleXMLElement Object

                (

                    [@attributes] => Array

                        (

                            [ItemNumber] => 00000088

                            [FormattedItemNumber] => 00000-088

                        )


                    [Category] =>   Bags/Luggage 123 

                )

如何获得 CatId 值?为什么缺少 cateId 值?


慕娘9325324
浏览 131回答 3
3回答

红糖糍粑

您可以使用以下代码段循环并获取它,请参阅内联文档以获取解释$xml1 = simplexml_load_file("test.xml") or die("Error: Cannot create object");foreach ($xml1->children() as $items1) { // children mean item&nbsp; &nbsp; echo ($items1->category['catid']); // for category tag get catid attribute}
随时随地看视频慕课网APP
我要回答