elementtree

Keep Existing Namespaces when overwriting XML file with ElementTree and Python

柔情痞子 提交于 2019-11-27 06:17:24
问题 I have an XML file in the following format <?xml version="1.0" encoding="utf-8"?> <foo> <bar> <bat>1</bat> </bar> <a> <b xmlns="urn:schemas-microsoft-com:asm.v1"> <c>1</c> </b> </a> </foo> I want to change the value of bat to '2' and change the file to this: <?xml version="1.0" encoding="utf-8"?> <foo> <bar> <bat>2</bat> </bar> <a> <b xmlns="urn:schemas-microsoft-com:asm.v1"> <c>1</c> </b> </a> </foo> I open this file by doing this tree = ET.parse(filePath) root = tree.getroot() I then change

Why does xml package modify my xml file in Python3?

非 Y 不嫁゛ 提交于 2019-11-27 05:36:45
I use the xml library in Python3.5 for reading and writing an xml-file. I don't modify the file. Just open and write. But the library modifes the file. Why is it modified? How can I prevent this? e.g. I just want to replace specific tag or it's value in a quite complex xml-file without loosing any other informations. This is the example file <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <movie> <title>Der Eisbär</title> <ids> <entry> <key>tmdb</key> <value xsi:type="xs:int" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">9321</value>

How do I use xml namespaces with find/findall in lxml?

眉间皱痕 提交于 2019-11-27 05:36:11
问题 I'm trying to parse content in an OpenOffice ODS spreadsheet. The ods format is essentially just a zipfile with a number of documents. The content of the spreadsheet is stored in 'content.xml'. import zipfile from lxml import etree zf = zipfile.ZipFile('spreadsheet.ods') root = etree.parse(zf.open('content.xml')) The content of the spreadsheet is in a cell: table = root.find('.//{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table') We can also go straight for the rows: rows = root.findall(

How to create <!DOCTYPE> with Python's cElementTree

╄→гoц情女王★ 提交于 2019-11-27 05:34:59
I have tried to use the answer in this question, but can't make it work: How to create "virtual root" with Python's ElementTree? Here's my code: import xml.etree.cElementTree as ElementTree from StringIO import StringIO s = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE tmx SYSTEM \"tmx14a.dtd\" ><tmx version=\"1.4a\" />' tree = ElementTree.parse(StringIO(s)).getroot() header = ElementTree.SubElement(tree,'header',{'adminlang': 'EN',}) body = ElementTree.SubElement(tree,'body') ElementTree.ElementTree(tree).write('myfile.tmx','UTF-8') When I open the resulting 'myfile.tmx' file, it

ElementTree and unicode

隐身守侯 提交于 2019-11-27 04:32:58
问题 I have this char in an xml file: <data> <products> <color>fumè</color> </product> </data> I try to generate an instance of ElementTree with the following code: string_data = open('file.xml') x = ElementTree.fromstring(unicode(string_data.encode('utf-8'))) and I get the following error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 185: ordinal not in range(128) (NOTE: The position is not exact, I sampled the xml from a larger one). How to solve it? Thanks 回答1:

Saving XML files using ElementTree

心已入冬 提交于 2019-11-27 04:28:22
I'm trying to develop simple Python (3.2) code to read XML files, do some corrections and store them back. However, during the storage step ElementTree adds this namespace nomenclature. For example: <ns0:trk> <ns0:name>ACTIVE LOG</ns0:name> <ns0:trkseg> <ns0:trkpt lat="38.5" lon="-120.2"> <ns0:ele>6.385864</ns0:ele> <ns0:time>2011-12-10T17:46:30Z</ns0:time> </ns0:trkpt> <ns0:trkpt lat="40.7" lon="-120.95"> <ns0:ele>5.905273</ns0:ele> <ns0:time>2011-12-10T17:46:51Z</ns0:time> </ns0:trkpt> <ns0:trkpt lat="43.252" lon="-126.453"> <ns0:ele>7.347168</ns0:ele> <ns0:time>2011-12-10T17:52:28Z</ns0

How to write XML declaration using xml.etree.ElementTree

坚强是说给别人听的谎言 提交于 2019-11-27 04:20:44
I am generating an XML document in Python using an ElementTree , but the tostring function doesn't include an XML declaration when converting to plaintext. from xml.etree.ElementTree import Element, tostring document = Element('outer') node = SubElement(document, 'inner') node.NewValue = 1 print tostring(document) # Outputs "<outer><inner /></outer>" I need my string to include the following XML declaration: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> However, there does not seem to be any documented way of doing this. Is there a proper method for rendering the XML declaration in

Using XPath in ElementTree

╄→гoц情女王★ 提交于 2019-11-27 03:39:30
My XML file looks like the following: <?xml version="1.0"?> <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2008-08-19"> <Items> <Item> <ItemAttributes> <ListPrice> <Amount>2260</Amount> </ListPrice> </ItemAttributes> <Offers> <Offer> <OfferListing> <Price> <Amount>1853</Amount> </Price> </OfferListing> </Offer> </Offers> </Item> </Items> </ItemSearchResponse> All I want to do is extract the ListPrice. This is the code I am using: >> from elementtree import ElementTree as ET >> fp = open("output.xml","r") >> element = ET.parse(fp).getroot() >> e = element.findall(

How to get all sub-elements of an element tree with Python ElementTree?

北慕城南 提交于 2019-11-27 02:50:15
问题 I want to find a way to get all the sub-elements of an element tree like the way ElementTree.getchildren() does, since getchildren() is deprecated since Python version 2.7, I don't want to use it anymore, though I can still use it currently. Thanks. 回答1: All sub-elements (descendants) of elem : all_descendants = list(elem.iter()) A more complete example: >>> import xml.etree.ElementTree as ET >>> a = ET.Element('a') >>> b = ET.SubElement(a, 'b') >>> c = ET.SubElement(a, 'c') >>> d = ET

Faithfully Preserve Comments in Parsed XML

删除回忆录丶 提交于 2019-11-27 02:41:50
问题 I'd like to preserve comments as faithfully as possible while manipulating XML. I managed to preserve comments, but the contents are getting XML-escaped. #!/usr/bin/env python # add_host_to_tomcat.py import xml.etree.ElementTree as ET from CommentedTreeBuilder import CommentedTreeBuilder parser = CommentedTreeBuilder() if __name__ == '__main__': filename = "/opt/lucee/tomcat/conf/server.xml" # this is the important part: use the comment-preserving parser tree = ET.parse(filename, parser) #