如何用PHP解析这个XML

时间:2023-01-15 00:07:21

Normally the XML files I have to parse are like this:

通常我要解析的XML文件是这样的:

<row id="1">
    <title>widget<title>
    <color>blue<color>
    <price>five<price>
</row>

Which I would then parse like this:

我会这样分析

$xmlstr_widget = file_get_contents($my_xml_feed);
$feed_widget = new SimpleXMLElement($xmlstr_widget);

foreach($feed_widget as $name) {
    $title = $name->title;
    $color = $name->color;
    $price = $price->price;
  }

Works great! But now I have xml in a bit different format and I am a little stumped since I don't have a whole lot of xml parsing experience:

工作好了!但是现在我有了不同格式的xml,我有一点困难,因为我没有大量的xml解析经验:

<Widget Title="large" Color="blue" Price="five"/>
<Widget Title="small" Color="red" Price="ten"/>

How do I drill into that a little further and get it parsed properly? I have tried a few things but no success.

我该如何进一步深入研究并对其进行恰当的分析呢?我尝试过一些东西,但没有成功。

So the problem is when I try something like below with the different xml feed, I cannot echo any results.

所以问题是,当我使用不同的xml提要尝试以下内容时,我无法响应任何结果。

foreach($feed_widget as $name) {
    $title = $name->title;
    $color = $name->color;
    $price = $price->price;
  }

4 个解决方案

#1


1  

You need to use the attributes() of the element.

您需要使用元素的属性()。

For example, you'd want to do

例如,你想做的

$feed_widget -> attributes() -> Color;

would give you "blue"

会给你“蓝色”

Resource: http://www.w3schools.com/xml/xml_attributes.asp

资源:http://www.w3schools.com/xml/xml_attributes.asp

#2


2  

You can use the attributes() method to get the list of attributes on an element:

可以使用attributes()方法获取元素上的属性列表:

foreach ($xml as $element) {
    foreach ($element->attributes() as $name => $value) {
        echo "$name = $value\n";
    }
}

Outputs:

输出:

Title = large
Color = blue
Price = five
Title = small
Color = red
Price = ten

#3


2  

You can access the attributes like you would access elements in an associative array:

您可以访问属性,就像访问关联数组中的元素一样:

foreach($feed_widget as $name) {
    $title = $name['Title'];
    $color = $name['Color'];
    $price = $name['Price'];
}

#4


1  

<Widget Title="large" Color="blue" Price="five"/>

is short-hand for

是速记

<Widget Title="large" Color="blue" Price="five"></Widget>

The Title="large" Color="blue" etc are ATTRIBUTES of the XML tag. The foreach statement you've provided in the question extracts the CONTENTS of the XML tag (what appears between the opening and closing tags). You won't get anything that way because the CONTENTS are a string of zero length.

Title="large" Color="blue"等是XML标签的属性。问题中提供的foreach语句提取XML标记的内容(在开始标记和结束标记之间出现的内容)。你不会得到任何东西,因为内容是一个零长度的字符串。

http://www.php.net/manual/en/simplexmlelement.attributes.php

http://www.php.net/manual/en/simplexmlelement.attributes.php

#1


1  

You need to use the attributes() of the element.

您需要使用元素的属性()。

For example, you'd want to do

例如,你想做的

$feed_widget -> attributes() -> Color;

would give you "blue"

会给你“蓝色”

Resource: http://www.w3schools.com/xml/xml_attributes.asp

资源:http://www.w3schools.com/xml/xml_attributes.asp

#2


2  

You can use the attributes() method to get the list of attributes on an element:

可以使用attributes()方法获取元素上的属性列表:

foreach ($xml as $element) {
    foreach ($element->attributes() as $name => $value) {
        echo "$name = $value\n";
    }
}

Outputs:

输出:

Title = large
Color = blue
Price = five
Title = small
Color = red
Price = ten

#3


2  

You can access the attributes like you would access elements in an associative array:

您可以访问属性,就像访问关联数组中的元素一样:

foreach($feed_widget as $name) {
    $title = $name['Title'];
    $color = $name['Color'];
    $price = $name['Price'];
}

#4


1  

<Widget Title="large" Color="blue" Price="five"/>

is short-hand for

是速记

<Widget Title="large" Color="blue" Price="five"></Widget>

The Title="large" Color="blue" etc are ATTRIBUTES of the XML tag. The foreach statement you've provided in the question extracts the CONTENTS of the XML tag (what appears between the opening and closing tags). You won't get anything that way because the CONTENTS are a string of zero length.

Title="large" Color="blue"等是XML标签的属性。问题中提供的foreach语句提取XML标记的内容(在开始标记和结束标记之间出现的内容)。你不会得到任何东西,因为内容是一个零长度的字符串。

http://www.php.net/manual/en/simplexmlelement.attributes.php

http://www.php.net/manual/en/simplexmlelement.attributes.php