无法访问XML节点中的@attributes

时间:2016-06-02 01:56:16

标签: php xml simplexml

我正在尝试访问以下XML中“条件”节点中的“field”元素:

<?xml version="1.0" encoding="utf-8"?>
<result>
    <product>
        <data>
            <field>spr_tech1</field>
            <value>S7</value>
            <criteria field="xfield_3">
                <criteria_list>Green</criteria_list>
                <criteria_list>Beige</criteria_list>
            </criteria>
        </data>
        <data>
            <field>spr_tech1</field>
            <value>S1</value>
            <criteria field="xfield_3">
                <criteria_list>Red</criteria_list>
                <criteria_list>Blue</criteria_list>
                <criteria_list>Yellow</criteria_list>
            </criteria>
        </data>
        <data>
            <field>spr_tech1</field>
            <value>S7</value>
            <criteria field="xfield_3">
                <criteria_list>Green</criteria_list>
            </criteria>
            <criteria field="tech_rt2">
                <criteria_list>Transistor</criteria_list>
            </criteria>
        </data>
    </product>
</result>

以下代码会导致以下错误: 致命错误:在非对象上调用成员函数attributes()

function parseXmlFile($filename)
{
    $xml = file_get_contents($filename);
    $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    $array = json_decode(json_encode($obj), true); // Convert to array}
    return $array;
}

$xform = parseXmlFile('transformations.xml');
foreach ($xform['product']['data'] as $data)
{
    echo (string)$data['field'] . '<br>';
    echo (string)$data['value'] . '<br>';

    foreach($data['criteria']->attributes() as $att => $val)
    { echo $att . ' = ' . $val . '<br>'; }
    echo $data['criteria']->attributes()->{'field'} . '<br>';

    foreach($data['criteria']['criteria_list'] as $att => $val)
    { echo $att . ' = ' . $val . '<br>'; }

    echo "-----------------------------------<br>";
}

print "<pre>";
print_r($xform);
print "</pre>";

我已经尝试了几种方法,你可以在代码中看到,但两种方法都不适用于我。

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

似乎问题是加载文件时。 我尝试了file_get_contents()见下文

$xml_content = file_get_contents('path to your XML file');
$xform = simplexml_load_string($xml_content);

foreach ($xform['product']['data'] as $data)
{
    echo (string)$data['field'] . '<br>';
    echo (string)$data['value'] . '<br>';

    foreach($data['criteria']->attributes() as $att => $val)
    { echo $att . ' = ' . $val . '<br>'; }
    echo $data['criteria']->attributes()->{'field'} . '<br>';

    foreach($data['criteria']['criteria_list'] as $att => $val)
    { echo $att . ' = ' . $val . '<br>'; }

    echo "-----------------------------------<br>";
}

print "<pre>";
print_r($xform);
print "</pre>";

答案 1 :(得分:1)

您的file_get_contents功能可以使用内置的simplexml_load_file()完全替换:

  • 首先加载simplexml_load_file的内容; simplexml_load_string vs LIBXML_NO_CDATA将为您执行此操作
  • 它强制(string)$node选项,这几乎肯定不是你需要的东西;要获取SimpleXML对象的文本内容(包括CDATA),请使用->name
  • 它将非常有用的SimpleXML对象转换为一个数组,该数组将丢失一半的数据,而且更糟糕的是

要了解SimpleXML,请查看the examples in the manual。使用['name']访问元素,使用$xform = simplexml_load_file('transformations.xml'); foreach ($xform->product->data as $data) { echo (string)$data->field . '<br>'; echo (string)$data->value . '<br>'; foreach($data->criteria->attributes() as $att => $val) { echo $att . ' = ' . $val . '<br>'; } echo $data->criteria['field'] . '<br>'; foreach($data->criteria->criteria_list as $number => $node) { echo $number . ' = ' . (string)$node . '<br>'; } echo "-----------------------------------<br>"; } 访问元素。

所以:

date