PHP XML数据缺少自定义属性数据

时间:2014-11-11 11:42:48

标签: php xml simplexml

我有以下格式的自定义XML:

<xml>
    <data>
        <node index='0.04, 0.17, 0.30, 0.43, 1.48, 2.01, 4.23, 4.36'>
            node data#1
        </node>
        <node index='0.07, 0.20, 0.33, 0.46, 1.51, 2.04, 4.27, 4.40'>
            node data#2
        </node>
        <node index='0.11, 0.24, 0.37, 0.50, 1.55, 2.08, 4.30, 4.43'>
            node data#3
        </node>
    </data>
</xml>

我使用以下简单的PHP代码来读取XML文件:

$file = 'test1.xml';
    if(file_exists($file) ){
        print "XML file found!";
        $xml = file_get_contents($file) or die('<br>Error loading XML file');
        $xml = simplexml_load_string($xml) or die('<br>Error loading XML file');
    }else{
        print "No XML file found!";
    }

    if($xml){
        print "<pre>";
        print_r($xml);
        print "</pre>";
    }

我的代码的输出显示了 SimpleXMLElement Object XML输出,除了我的索引属性数据源XML缺失。输出如下所示:

[data] => SimpleXMLElement Object
(
    [node] => Array
        (
            [0] => 
                node data#1
            [1] => 
                node data#2
            [2] => 
                node data#3
        )
)

它完全缺少我的节点索引属性数据。

3 个答案:

答案 0 :(得分:1)

这是打算让print_r不显示属性

这是一个关于如何访问属性

的示例
<?php
$xml='<root><itm index="data1">node</itm><itm index="data2">node</itm></root>';

$sxml =  simplexml_load_string($xml);

var_dump($sxml);

echo $sxml->itm[0]['index'];

foreach($sxml->itm as $itm){
    echo $itm['index'];
}

请参阅手册:

http://php.net/manual/de/simplexmlelement.attributes.php

答案 1 :(得分:0)

$simple = "<xml>
    <data>
        <node index='0.04, 0.17, 0.30, 0.43, 1.48, 2.01, 4.23, 4.36'>
            node data#1
        </node>
        <node index='0.07, 0.20, 0.33, 0.46, 1.51, 2.04, 4.27, 4.40'>
            node data#2
        </node>
        <node index='0.11, 0.24, 0.37, 0.50, 1.55, 2.08, 4.30, 4.43'>
            node data#3
        </node>
    </data>
</xml>";

$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "<pre>";
print_r($vals);

为简化结果,您可以使用

 $p = xml_parser_create();
    xml_parse_into_struct($p, $simple, $vals, $index);
    xml_parser_free($p);
    $array =array();
    foreach($vals as $v) {
        if(strtolower($v['type']) == 'complete') {
            $array[] = array('attributes'=>$v['attributes'],'value'=>$v['value']);      
        }
    }

echo "<pre>";
print_r($array);

答案 2 :(得分:0)

不,它不在print_r()内,但你可以访问它们,它不会丢失:

$xml_string = <<<XML
<xml>
    <data>
        <node index='0.04, 0.17, 0.30, 0.43, 1.48, 2.01, 4.23, 4.36'>
            node data#1
        </node>
        <node index='0.07, 0.20, 0.33, 0.46, 1.51, 2.04, 4.27, 4.40'>
            node data#2
        </node>
        <node index='0.11, 0.24, 0.37, 0.50, 1.55, 2.08, 4.30, 4.43'>
            node data#3
        </node>
    </data>
</xml>
XML;

$xml = simplexml_load_string($xml_string);

$first_node = $xml->data->node[0];
$attrs = (string) $first_node->attributes()->index;
echo $attrs;

或者当然要循环每个节点:

$xml = simplexml_load_string($xml_string);
$nodes = $xml->data->node;
foreach($nodes as $node) {
    echo (string) $node . '<br/>';
    $attrs = (string) $node->attributes()->index;
    echo $attrs . '<br/>';
    $numbers = explode(', ', $attrs);
    print_r($numbers);
    echo '<hr/>';
}

Sample Output