2011-09-17 117 views
0

我有一個很大的問題(至少對我來說))與SimpleXML分析器。錯誤是:PHP 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)文件:

<?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"); 

任何答案是讚賞。提前致謝。 ;)

回答

6

在標籤名稱中使用的數字是好的 - 但數量開始也不行......

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

XML命名規則

XML元素必須遵循以下命名規則:

  • 名稱可以包含字母,數字和其他字符
  • 名稱不能爲星號噸以數字或標點字符
  • 名稱不能以字母XML啓動(或XML或XML等)
  • 名稱不能包含空格

可以使用任何名稱,沒有的話將被保留。

1

您不能將數字作爲元素名稱。來自Wikipedia article on XML

元素標記區分大小寫;開始和結束標籤必須完全匹配。「#$%&'()* +,/; < =>?@ [] ^`{|}〜,也不是空格字符,並且不能以 - ,。,或者數字位數。

0

好definetely你不應該的名字開始與一些你的標籤。這是一個問題在這裏。我會建議你改變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> 

那麼你會proccess這個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