elementtree

Why does xml package modify my xml file in Python3?

旧街凉风 提交于 2019-11-26 08:38:18
问题 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\

access ElementTree node parent node

↘锁芯ラ 提交于 2019-11-26 02:21:28
问题 I am using the builtin Python ElementTree module. It is straightforward to access children, but what about parent or sibling nodes? - can this be done efficiently without traversing the entire tree? 回答1: There's no direct support in the form of a parent attribute, but you can perhaps use the patterns described here to achieve the desired effect. The following one-liner is suggested (from the linked-to post) to create a child-to-parent mapping for a whole tree: parent_map = dict((c, p) for p

Using Python Iterparse For Large XML Files

那年仲夏 提交于 2019-11-26 02:08:01
问题 I need to write a parser in Python that can process some extremely large files ( > 2 GB ) on a computer without much memory (only 2 GB). I wanted to use iterparse in lxml to do it. My file is of the format: <item> <title>Item 1</title> <desc>Description 1</desc> </item> <item> <title>Item 2</title> <desc>Description 2</desc> </item> and so far my solution is: from lxml import etree context = etree.iterparse( MYFILE, tag=\'item\' ) for event, elem in context : print elem.xpath( \'description

Parsing XML with namespace in Python via &#39;ElementTree&#39;

房东的猫 提交于 2019-11-25 23:57:32
问题 I have the following XML which I want to parse using Python\'s ElementTree : <rdf:RDF xml:base=\"http://dbpedia.org/ontology/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns=\"http://dbpedia.org/ontology/\"> <owl:Class rdf:about=\"http://dbpedia.org/ontology/BasketballLeague\"> <rdfs:label xml:lang=\"en\">basketball league</rdfs

Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method “find”, “findall”

本小妞迷上赌 提交于 2019-11-25 21:46:50
I want to use the method of "findall" to locate some elements of the source xml file in the ElementTree module. However, the source xml file (test.xml) has namespace. I truncate part of xml file as sample: <?xml version="1.0" encoding="iso-8859-1"?> <XML_HEADER xmlns="http://www.test.com"> <TYPE>Updates</TYPE> <DATE>9/26/2012 10:30:34 AM</DATE> <COPYRIGHT_NOTICE>All Rights Reserved.</COPYRIGHT_NOTICE> <LICENSE>newlicense.htm</LICENSE> <DEAL_LEVEL> <PAID_OFF>N</PAID_OFF> </DEAL_LEVEL> </XML_HEADER> The sample python code is below: from xml.etree import ElementTree as ET tree = ET.parse(r"test