RatDon
RatDon

Reputation: 3543

php simplexml showing unexpected error

i'm trying to create a xml file with the following code:

$xmlfile='template.xml';
$xml = simplexml_load_file($xmlfile);
$xml->asXML('12801.xml');

while template.xml file contains this:

<?xml version="1.0" standalone="yes"?>
<posts>
    <1president/>
    <2vice-president/>
    <3secretary/>
    <4assistant-secretary/>
    <5science-secretary/>
    <6dramatic-secretary/>
    <7athletic-secretary/>
</posts>

it's throwing a lot of errors saying:

Warning: simplexml_load_file(): template.xml:3: parser error : StartTag: invalid element name in try.php on line 3

what is the problem here? as much i know, the xml in template.xml seems to be valid.

Upvotes: 1

Views: 328

Answers (1)

Amal Murali
Amal Murali

Reputation: 76666

XML tags cannot start with numbers. It should always start with one of the following characters:

[A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

Something like this should work:

<posts>
    <president/>
    <vice-president/>
    <secretary/>
    <assistant-secretary/>
    <science-secretary/>
    <dramatic-secretary/>
    <athletic-secretary/>
</posts>

You can easily verify this by using an online XML validator, such as this one.

See the documentation here: http://www.w3.org/TR/REC-xml/#NT-NameStartChar

Upvotes: 4

Related Questions