Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中解析XML错误_Php_Xml_Parsing - Fatal编程技术网

在PHP中解析XML错误

在PHP中解析XML错误,php,xml,parsing,Php,Xml,Parsing,在过去,我用标记加载xmlfile,并像这样执行foreach循环 foreach ( $xml->station as $item ) { ... }> 现在我有了一个像这样的新结构,它不再工作了。 你能帮帮我吗 <stations> <station id="1" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147"

在过去,我用标记加载xmlfile,并像这样执行foreach循环

foreach ( $xml->station as $item )
{
...
}>
现在我有了一个像这样的新结构,它不再工作了。 你能帮帮我吗

<stations>
<station id="1" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION"/>

<station id="2" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION"/>
</stations>
提前多谢!:-

添加了simplexml\u加载\u字符串 如果需要加载文件$xml=simplexml_加载文件'test.xml'; 添加的xml doctype行丢失。 示例如下:
$xml_string = '<?xml version="1.0" encoding="UTF-8"?>
<stations>
  <station id="1" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION" />
  <station id="2" name="Raumschiff2" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION" />
</stations>';

$xml = simplexml_load_string($xml_string);    
//print_r($xml);

// iterate over the SimpleXMLElement Object    
foreach($xml->station as $station) {

    // you need to take the attributes into account
    foreach ($station->attributes() as $attributeName => $attributeValue) {
        printf("\n%s => %s", $attributeName, $attributeValue);
    }

    echo "<hr>";
}
下一个问题是:如何迭代该对象或数组

已经答复:


1有什么错误吗?你会发代码吗?3年发布的XML不是有效的XML。它没有在过去你有相同的文件,但没有标签?我不能发布完整的XML文件,因为它的大小为1MB,所以它非常大。这只是XML文件的一个小快照。原始文件始于我从我们的客户处下载的文件,过去XML文件是随附的,现在是随附的/>现在我得到了这个输出[stations]=>SimpleXMLElement对象[station]=>Array[0]=>SimpleXMLElement对象[@attributes]=>Array[id]=>2[name]=>Raumschiff 1[strasse]=>Musterstrasse 1[plz]=>11011[ort]=>Berlin[lat]=>11.11111[lng]=>0.08147[operator]=>NEO[telefon]=>0800 11111现在我尝试使用foreach$array->station->id作为$item访问属性,但它不起作用。这只是一个对象或数组值的迭代。我已经相应地更新了我的答案。