1

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.

7
  • 1
    Element names are case sensitive. Must begin with letter or underscore(_). After initial character following are allowed: digits period(.) hyphen(-) underscore(_) colon(: - legal but should not be used except for namespaces NO other characters are allowed like #, @, $, %.... w3.org/TR/REC-xml/#NT-NameChar Commented Aug 18, 2013 at 21:14
  • i created the template.xml file with simplexml only. it didn't show any error at that time. Commented Aug 18, 2013 at 21:16
  • Oh, so I'm sorry the w3c RFC for XML is incorrect in its definition, erroneously stating that digits are invalid starct characters for a tag name Commented Aug 18, 2013 at 21:19
  • @MarkBaker dude. no need to be offensive. i've said what i've experienced. i created the template.xml file with following code only which was a success. $result3 = mysql_query("SELECT * FROM post") or die(mysql_error()); $xml=new SimpleXMLElement("<posts></posts>");; while($row3 = mysql_fetch_array($result3)) $newchild = $xml->addChild($row3['post']); $xml->asXML("vote/template.xml"); Commented Aug 18, 2013 at 21:29
  • 2
    simplexml is simple, and designed for handling the simplest of xml files: it's not written to handle every last complexity of xml. In fact has problems with anything more than the simplest xml files (e.g. namespaces become incredibly complicated to handle when working with simplexml) Commented Aug 18, 2013 at 22:05

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.