Ошибка PHP SimpleXML - PullRequest
       13

Ошибка PHP SimpleXML

0 голосов
/ 17 сентября 2011

У меня большая проблема (по крайней мере, для меня;)) с парсером SimpleXML.Ошибка:

Warning: simplexml_load_file(): file.meta:16: parser error : StartTag: invalid element name in index.php on line 89Warning: simplexml_load_file(): <335> in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: simplexml_load_file(): file.meta:18: parser error : expected '>' in index.php on line 89Warning: simplexml_load_file(): in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: simplexml_load_file(): file.meta:18: parser error : Opening and ending tag mismatch: Sequence line 15 and unparseable in index.php on line 89Warning: simplexml_load_file(): in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: simplexml_load_file(): file.meta:19: parser error : StartTag: invalid element name in index.php on line 89Warning: simplexml_load_file(): <337> in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: simplexml_load_file(): file.meta:21: parser error : expected '>' in index.php on line 89Warning: simplexml_load_file(): in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: simplexml_load_file(): file.meta:21: parser error : Opening and ending tag mismatch: MetaAttack line 2 and unparseable in index.php on line 89Warning: simplexml_load_file(): in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: simplexml_load_file(): file.meta:21: parser error : Extra content at the end of the document in index.php on line 89Warning: simplexml_load_file(): in index.php on line 89Warning: simplexml_load_file(): ^ in index.php on line 89Warning: 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");

Любой ответ приветствуется.Заранее спасибо.;)

Ответы [ 3 ]

6 голосов
/ 17 сентября 2011

Использование чисел в именах тегов - это нормально, но начинать с номера - не хорошо ...

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

Правила именования XML

Элементы XML должны соответствовать следующим правилам именования:

  • Имена могут содержать буквы, цифры и другие символы
  • Имена не могут начинаться с цифры или знака пунктуации
  • Имена не могут начинаться с букв xml (или XML, или Xml и т. Д.)
  • Имена не могут содержать пробелы

Можно использовать любое имя, слова не зарезервированы.

1 голос
/ 17 сентября 2011

Вы не можете иметь номера в качестве имен элементов. Из статьи Википедии о XML :

Теги элемента чувствительны к регистру; теги начала и конца должны точно совпадать. Имена тегов не могут содержать ни одного из символов! "# $% & '() * +, /; <=>? @ [] ^` {|} ~, Ни пробела, и не могут начинаться с -,., или цифра.

0 голосов
/ 17 сентября 2011

Определенно, вы не должны называть свои теги, начиная с цифры.Это проблема здесь.Я бы порекомендовал вам изменить структуру 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 
...