首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP SimpleXML错误

PHP SimpleXML错误
EN

Stack Overflow用户
提问于 2011-09-17 16:28:49
回答 3查看 10K关注 0票数 0

我对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

我用谷歌搜索过了,但一无所获。我还搜索了XML标记声明(可能禁止声明数字标记),但什么也没找到。您可以在下面找到我的xml (.meta)文件:

代码语言:javascript
复制
<?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行:

代码语言:javascript
复制
$ma = simplexml_load_file("file.meta");

任何答案都是值得感谢的。提前谢谢。;)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-17 16:35:27

在标记名中使用数字是可以的-但以数字开头是不可以的……

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

XML命名规则

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

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

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

票数 6
EN

Stack Overflow用户

发布于 2011-09-17 16:35:52

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

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

票数 1
EN

Stack Overflow用户

发布于 2011-09-17 16:56:40

当然,你不应该以数字开头命名你的标签。这是一个问题。我建议您更改XML结构,因为它现在处理起来一点也不方便。例如:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
$ma = simplexml_load_file("file.meta");
$sequence = $ma->xpath('Sequence/value');

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

它将输出:

代码语言:javascript
复制
Index: 335, value: 1315815794 
Index: 336, value: 1315815814 
Index: 337, value: 1315815804 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7453529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档