python 2.7 - how to use etree.write() to output a xml file that doesn't have default ns? -
so have xml file needs change. after modification, want write new content new file, output.xml. used write() method, lot ns# in front of each line. figured how rid of these ns# using _namespace_map, how preserve previous xmlns? example, original xml file :
<?xml version='1.0' encoding='utf-8'?> <server xmlns="urn:jboss:domain:1.2"> <extensions> </extensions> <profile> <subsystem xmlns="urn:jboss:domain:logging:1.1"> </subsystem> <subsystem xmlns="urn:jboss:domain:ee:1.0"> </subsystem> </profile> </server>
currently i'm getting:
<ns0:server xmlns:ns0="urn:jboss:domain:1.2" xmlns:ns1="urn:jboss:domain:logging:1.1" xmlns:ns2="urn:jboss:domain:ee:1.0"> <ns0:extensions> </ns0:extensions> <ns0:profile> <ns1:subsystem> </ns1:subsystem> <ns2:subsystem> </ns2:subsystem> </ns0:profile> </ns0:server>
what want have in output.xml:
<?xml version='1.0' encoding='utf-8'?> <server xmlns="urn:jboss:domain:1.2"> <extensions> </extensions> <profile> <subsystem xmlns="urn:jboss:domain:logging:1.1"> </subsystem> <subsystem xmlns="urn:jboss:domain:ee:1.0"> </subsystem> </profile> </server>
i didn't show have modified inside tags, since it's not related question. thanks!
it can hard make elementtree behave wanted. suggestion use lxml instead, if possible.
lxml library similar elementtree, more features. not muck namespace declarations.
this code parses original xml , writes virtually unchanged new file:
from lxml import etree doc = etree.parse("original.xml") etree.elementtree(doc.getroot()).write("out.xml", encoding="utf-8", xml_declaration=true)
Comments
Post a Comment