web-dev-qa-db-fra.com

Émettre des spécifications d'espace de noms avec Elementtree In Python

J'essaie d'émettre un fichier XML avec Element-Tree contenant une déclaration et des espaces de noms XML. Voici mon exemple de code:

from xml.etree import ElementTree as ET
ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("STUFF")
body = ET.SubElement(root, "MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,
           method="xml" )

Cependant, ni le <?xml Tag sort ni aucune information d'espace de noms/préfixe. Je suis plus qu'un peu confus ici.

29
Paul Nathan

Bien que le docs dire autrement, je n'ai pu obtenir qu'un <?xml> Déclaration en spécifiant à la fois le XML_Declaration et le codage.

Vous devez déclarer des nœuds dans l'espace de noms que vous avez enregistré pour obtenir l'espace de noms sur les nœuds du fichier. Voici une version fixe de votre code:

from xml.etree import ElementTree as ET
ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,encoding='utf-8',
           method="xml")

Sortie (page.xml)

<?xml version='1.0' encoding='utf-8'?><com:STUFF xmlns:com="http://www.company.com"><com:MORE_STUFF>STUFF EVERYWHERE!</com:MORE_STUFF></com:STUFF>

Elementtree n'est pas jolie-impression non plus. Voici une sortie jolie imprimée:

<?xml version='1.0' encoding='utf-8'?>
<com:STUFF xmlns:com="http://www.company.com">
    <com:MORE_STUFF>STUFF EVERYWHERE!</com:MORE_STUFF>
</com:STUFF>

Vous pouvez également déclarer un espace de noms par défaut et ne pas avoir besoin d'enregistrer un:

from xml.etree import ElementTree as ET

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,encoding='utf-8',
           method="xml",default_namespace='http://www.company.com')

Sortie (espacement joli-impression est le mien)

<?xml version='1.0' encoding='utf-8'?>
<STUFF xmlns="http://www.company.com">
    <MORE_STUFF>STUFF EVERYWHERE!</MORE_STUFF>
</STUFF>
34
Mark Tolonen

Je n'ai jamais pu obtenir le <?xml Étiquetez parmi les bibliothèques d'arbres d'élément de manière programmée afin que je vous suggère d'essayer quelque chose comme ça.

from xml.etree import ElementTree as ET
root = ET.Element("STUFF")
root.set('com','http://www.company.com')
body = ET.SubElement(root, "MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

f = open('page.xml', 'w')
f.write('<?xml version="1.0" encoding="UTF-8"?>' + ET.tostring(root))
f.close()

Non std lib python Elementtree implémentations peut avoir des moyens différents de spécifier des espaces de noms, de sorte que si vous décidez de passer à LXML, la façon dont vous déclarez que ceux-ci seront différents.

8
Philip Southam