从XML文件中获取特定结果

时间:2014-07-10 07:22:58

标签: php xml loops foreach

您好我正在尝试从XML文件中提取一些数据。但是我的解决方案无法正常工作。

XML文件:

<?xml version='1.0' encoding='UTF-8'?>
<m4n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="datafeeds/ext/m4n-datafeeds.xsd">
<dataHeader>
<exportType>stream</exportType>
<exportId>2000433</exportId>
<rows>40434</rows>
<lastChecked>2014-07-02 10:12:24.41</lastChecked>
<lastUpdated>2014-07-02 10:13:36.09</lastUpdated>
<parserLocale>nl_NL</parserLocale>
<streamCurrency>EUR</streamCurrency>
<name>VALUE</name>
<description><![CDATA[]]></description>
</dataHeader>

<data>
<record>
    <recordHash><![CDATA[-2147395708]]></recordHash>
    <column name="url"><![CDATA[VALUE]]></column>
    <column name="title"><![CDATA[VALUE]]></column>
    <column name="description"><![CDATA[VALUE]]></column>
    <column name="offerid"><![CDATA[VALUE]]></column>
    <column name="image"><![CDATA[VALUE]]></column>
    <column name="price"><![CDATA[VALUE]]></column>
    <column name="category"><![CDATA[VALUE]]></column>
    <column name="subcategory"><![CDATA[VALUE]]></column>
    <column name="stock" />
    <column name="timetoship"><![CDATA[VALUE]]></column>
    <column name="ean"><![CDATA[VALUE]]></column>
    <column name="price_shipping"><![CDATA[VALUE]]></column>
    <column name="price_old" />
    <column name="vendor"><![CDATA[VALUE]]></column>
    <column name="category_path" />
    <column name="description2"><![CDATA[VALUE]]></column>
    <column name="largeimage"><![CDATA[VALUE]]></column>
    <column name="author"><![CDATA[VALUE]]></column>
    <column name="column0"><![CDATA[VALUE]]></column>
    <column name="column1"><![CDATA[VALUE]]></column>
    <column name="column2"><![CDATA[VALUE]]></column>
    <column name="column3"><![CDATA[VALUE]]></column>
    <column name="column4"><![CDATA[VALUE]]></column>
    <column name="column5"><![CDATA[VALUE]]></column>
    <column name="column6"><![CDATA[VALUE]]></column>
    <column name="column7"><![CDATA[VALUE]]></column>
    <column name="column8"><![CDATA[VALUE]]></column>
    <column name="column9"><![CDATA[VALUE]]></column>
    <column name="sku"><![CDATA[VALUE]]></column>
    <column name="size"><![CDATA[VALUE]]></column>
    <column name="height"><![CDATA[VALUE]]></column>
</record>
</data></m4n>

我想要提取的内容如下: url = VALUE title = VALUE 等等,基本上我想要列的名称和列的值。

我尝试了以下方法:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'url');
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

foreach($xml->data as $data)
{
    foreach($data->record as $record)
    {
        foreach($record->column as $column)
        {
            foreach($column->attributes() as $key => $value)
            {
                echo $key." - ".$value."<br/>";
            }
        }
    }
}

有人可以看看

1 个答案:

答案 0 :(得分:1)

好的,你需要像这样提取CDATA值:

foreach($xml->data as $data)
{
    foreach($data->record as $record)
    {
        foreach($record->column as $column)
        {
          echo $column->attributes()->{0}." - ".(string)$column."<br/>"; // fetch first attribute and the CDATA value

        }
    }
}

$column转换为字符串。

CodePAD