PHP SimpleXML错误

时间:2011-09-17 08:28:49

标签: php simplexml

我有一个很大的问题(至少对我而言;))使用SimpleXML解析器。错误是:

Warning: simplexml_load_file(): file.meta:16: parser error : StartTag: invalid element name in index.php on line 89
Warning: simplexml_load_file(): <335> in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89
Warning: simplexml_load_file(): file.meta:18: parser error : expected '>' in index.php on line 89
Warning: simplexml_load_file(): in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89 Warning: simplexml_load_file(): file.meta:18: parser error : Opening and ending tag mismatch: Sequence line 15 and unparseable in index.php on line 89
Warning: simplexml_load_file(): in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89
Warning: simplexml_load_file(): file.meta:19: parser error : StartTag: invalid element name in index.php on line 89
Warning: simplexml_load_file(): <337> in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89
Warning: simplexml_load_file(): file.meta:21: parser error : expected '>' in index.php on line 89
Warning: simplexml_load_file(): in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89
Warning: simplexml_load_file(): file.meta:21: parser error : Opening and ending tag mismatch: MetaAttack line 2 and unparseable in index.php on line 89
Warning: simplexml_load_file(): in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89
Warning: simplexml_load_file(): file.meta:21: parser error : Extra content at the end of the document in index.php on line 89
Warning: simplexml_load_file(): in index.php on line 89
Warning: simplexml_load_file(): ^ in index.php on line 89
Warning: Invalid argument supplied for foreach() in index.php on line 97

我用Google搜索但没有得到任何结果。我也搜索了XML标签声明(也许它被禁止声明一个数字标签)但什么也没找到。您可以在下面找到我的xml(.meta)文件:

<?xml version="1.0"?>                                                                                                                                                   
<MA>
    <Count>
        3
    </Count>
    <Duration>
        34
    </Duration>
    <End>
        1315815814
    </End>
    <Start>
        1315815780
    </Start>
    <Sequence>
        <335>
            1315815794
        </335>
        <337>
            1315815814
        </337>
        <336>
            1315815804
        </336>
    </Sequence>
</MA>

第89行:

$ma = simplexml_load_file("file.meta");

任何答案都表示赞赏。提前致谢。 ;)

3 个答案:

答案 0 :(得分:6)

在标签名称中使用数字是可以的 - 但从数字开始不行......

http://www.w3schools.com/xml/xml_elements.asp

XML命名规则

XML元素必须遵循以下命名规则:

  • 名称可以包含字母,数字和其他字符
  • 名称不能以数字或标点符号开头
  • 名称不能以字母xml(或XML,或Xml等)
  • 开头
  • 名称不能包含空格

可以使用任何名称,不保留任何单词。

答案 1 :(得分:1)

您不能将数字作为元素名称。来自Wikipedia article on XML

  

元素标签区分大小写;开始和结束标记必须完全匹配。标记名称不能包含任何字符!“#$%&amp;'()* +,/;&lt; =&gt;?@ [] ^`{|}〜,也不是空格字符,并且不能以 - 开头, 。,或数字。

答案 2 :(得分:0)

很明确,您不应该以数字开头命名标签。这是一个问题。我建议你改变XML结构,因为它现在根本不方便处理。例如:

<?xml version="1.0"?>
<MA>
    <Count>3</Count>
    <Duration>34</Duration>
    <End>1315815814</End>
    <Start>1315815780</Start>
    <Sequence>
        <value index="335">1315815794</value>
        <value index="336">1315815814</value>
        <value index="337">1315815804</value>
    </Sequence>
</MA>

然后,您可以使用代码轻松处理此XML:

$ma = simplexml_load_file("file.meta");
$sequence = $ma->xpath('Sequence/value');

foreach ($sequence as $el)
{
    echo "Index: {$el['index']}, value: $el <br>";
}

将输出:

Index: 335, value: 1315815794 
Index: 336, value: 1315815814 
Index: 337, value: 1315815804