How to create an XML tag with a number?

I am using the 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 can successfully create an XML document, but when I try to read in the XML file again, I get a ParseError. For my purposes, it doesn't matter if the XML document is poorly formed. I just need to run a tag with a number. Any idea how to do this?

Here's what I've 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'm trying to put into an XML file:

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

      

Each element in the list must be used as an ElementTree tag. The problem occurs when I reach "1.0".

If you can't answer the first question, do you know of any other module that will do pretty much the same thing, but let me have a tag that starts with a number? ElementTree is fantastic, I just need it to work and then I can move on.

+3


source to share


1 answer


XML element names cannot start 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 the file has a tag that starts with a number, then the file is not an XML document. To create such a file, do not expect support from any compatible XML library . To create a file with tags that started with numbers, you have to work at the text level, but really it's best not to go against the grain - just start your tags with letters so that you can use tools that work on well-formed XML .

+4


source







All Articles