3

I'm using ElementTree API to read and write to an XML document. When I try to add a tag that starts with a number, the XML file is no longer valid. Using import xml.etree.cElementTree as ET, I am successfully able to create the XML document, but when I try to read in the XML file again, I get a ParseError. For my purposes, it does not matter if the XML document is not well formed. I just need to be able to start a tag with a number. Any idea how to do this?

This is what I have tried:

from lxml import etree
parser = etree.XMLParser(recover=True)
tree = ET.parse('xmldoc.xml')
root = tree.getroot()
xmlstring = ET.tostring(root)
etree.fromstring(xmlstring, parser=parser)

If I use this, I get this error:

ValueError: Invalid tag name u'1.0'

after trying to do this:

            inputowner = raw_input("Enter owner for " + ls[i] + ": ")
            child = ET.SubElement(prev , ls[i], owner = inputowner)
            prev = child
            prevowner = inputowner

Here is the list I am trying to put into an XML file:

['components', 'rel', 'core.slpi', '1.0', 'blluuses', 'i2c', 'src', 'logs', 'I2cUlog.c']

Each item in the list should be used as the ElementTree tag. The problem arises when I reach '1.0'.

If unable to answer first question, do you know of any other module that will do virtually the same thing but allow me have a tag that starts with a number? ElementTree is fantastic, I just need this one thing to work and then I can move on.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
windfallisland
  • 79
  • 1
  • 11

1 Answers1

4

XML element names cannot begin with a number:

STag       ::=      '<' Name (S Attribute)* S? '>'
Name       ::=      NameStartChar (NameChar)*
NameStartChar      ::=      ":" | [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]

If a file has a tag that begins with a number, then the file is not an XML document. In order to create such a file, do not expect support from any conformant XML library. In order to create a file with tags that started with numbers, you'd have to operate at the text level, but, really, you're better not going against the grain -- just start your tags with letters so that you can leverage tools that operate on well-formed XML.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • For my task, I can't really modify the elements in the list. They need to be exact. I've been doing research all morning though, perhaps you're right in that I should try a different approach. Thanks for the response. – windfallisland Jul 06 '15 at 18:49
  • I would absolutely change the approach. You're welcome. – kjhughes Jul 06 '15 at 19:05
  • 1
    "For my purposes, it does not matter if the XML document is not well formed." If it's not well-formed, then it's not XML. You can use any old data format that takes your fancy, but please don't call it XML or ask questions about it on an XML forum. And of course, don't expect to use an XML parser to read it. – Michael Kay Jul 06 '15 at 21:46