elementtree

Python: namespaces in xml ElementTree (or lxml)

筅森魡賤 提交于 2019-12-01 17:34:29
I want to retrieve a legacy xml file, manipulate and save it. Here is my code: from xml.etree import cElementTree as ET NS = "{http://www.somedomain.com/XI/Traffic/10}" def fix_xml(filename): f = ET.parse(filename) root = f.getroot() eventlist = root.findall("%(ns)Event" % {'ns':NS }) xpath = "%(ns)sEventDetail/%(ns)sEventDescription" % {'ns':NS } for event in eventlist: desc = event.find(xpath) desc.text = desc.text.upper() # do some editting to the text. ET.ElementTree(root, nsmap=NS).write("out.xml", encoding="utf-8") shorten_xml("test.xml") The file I load contains: xmlns="http://www

Python: namespaces in xml ElementTree (or lxml)

删除回忆录丶 提交于 2019-12-01 16:35:20
问题 I want to retrieve a legacy xml file, manipulate and save it. Here is my code: from xml.etree import cElementTree as ET NS = "{http://www.somedomain.com/XI/Traffic/10}" def fix_xml(filename): f = ET.parse(filename) root = f.getroot() eventlist = root.findall("%(ns)Event" % {'ns':NS }) xpath = "%(ns)sEventDetail/%(ns)sEventDescription" % {'ns':NS } for event in eventlist: desc = event.find(xpath) desc.text = desc.text.upper() # do some editting to the text. ET.ElementTree(root, nsmap=NS).write

Editing values in a xml file with Python

主宰稳场 提交于 2019-12-01 14:45:18
Hey. I want to have a config.xml file for settings in a Python web app. I made car.xml manually. It looks like this: <car> <lights> <blinkers>off</blinkers> </lights> </car> Now I want to see whether the blinkers are on or off, using xml.etree.ElementTree . import xml.etree.ElementTree as ET tree = ET.parse('car.xml') blinkers = tree.findtext('lights/blinkers') print blinkers > off Now I want to turn the blinkers on and off, how can I do this? XML is a rather poor way of storing configuration settings. For one, XML is not exactly human friendly in the context of settings. In the Python

nodejs elementtree npm xml parsing and merging

一笑奈何 提交于 2019-12-01 13:55:22
I have a question on same module as I posted last time in below post. Seems like, it is quite difficult to deal with XML parsing. With elementTree having very mearge documentation. I have a below template_XML file: <?xml version="1.0" encoding="UTF-8"?> <Tailoring xmlns="http://checklists.nist.gov/xccdf/1.2" id="xccdf_org.open-scap_tailoring_example"> <status>incomplete</status> <version time="2013-01-15T16:00:00.000+02:00">1.0</version> <Profile id="PlaceHolder"> <title>Tailoring for py_test</title> <select idref="xccdf_rule_name" selected="true"/> </Profile> </Tailoring> And a below sample

Why is elementtree.ElementTree.iterparse using so much memory?

落花浮王杯 提交于 2019-12-01 13:39:25
I am using elementtree.ElementTree.iterparse to parse a large (371 MB) xml file. My code is basically this: outf = open('out.txt', 'w') context = iterparse('copyright.xml') context = iter(context) dummy, root = context.next() for event, elem in context: if elem.tag == 'foo': author = elem.text elif elem.tag == 'bar': if elem.text is not None and 'bat' in elem.text.lower(): outf.write(elem.text + '\n') elem.clear() #line A root.clear() #line B My question is two-fold: First - Do I need both A and B (see code snippet comments)? I was told that root.clear() clears unnecessary children so memory

Editing values in a xml file with Python

感情迁移 提交于 2019-12-01 12:34:49
问题 Hey. I want to have a config.xml file for settings in a Python web app. I made car.xml manually. It looks like this: <car> <lights> <blinkers>off</blinkers> </lights> </car> Now I want to see whether the blinkers are on or off, using xml.etree.ElementTree . import xml.etree.ElementTree as ET tree = ET.parse('car.xml') blinkers = tree.findtext('lights/blinkers') print blinkers > off Now I want to turn the blinkers on and off, how can I do this? 回答1: XML is a rather poor way of storing

DATEXII XML file to DataFrame in Python

不羁岁月 提交于 2019-12-01 12:11:45
问题 The last couple of days I have been trying to open and read a certain XML file (in DATEXII format), but have not succeeded so far. It is about traffic data from the NDW Open Data website (Dutch Databank for Road and Traffic Data), hyperlink for the source of the XML files. The head of the tree is like in this picture and continues like this, see also snippet below. Though these together only form a very small part of the data. <?xml version="1.0"?> - <soapenv:Envelope xmlns:_0="http://datex2

ElementTree findall 'or' operator

半腔热情 提交于 2019-12-01 05:44:38
If I have an xml file like this: <root> <item> <prop>something</prop> </item> <test> <prop>something</prop> </test> <test2> <prop>something</prop> </test2> </root> I can use xmlTree.getroot().findall("item") to get all of the 'item' elements. How would I get all of the 'item' OR 'test' elements? I want something like: xmlTree.getroot().findall("item or test") I didn't see anything like this in the examples in the documentation. Any ideas? Since ElementTree from stdlib provides only limited xpath support , you can use | xpath OR operator only if you are using lxml : from lxml import etree as ET

Python - Element Tree is removing the XML declaration

假如想象 提交于 2019-12-01 03:14:15
I'm writing some XML with element tree. I'm giving the code an empty template file that starts with the XML declaration: <?xml version= "1.0"?> when ET has finished making its changes and writes the completed XML its stripping out the declarion and starting with the root tag. How can I stop this? Write call: ET.ElementTree(root).write(noteFile) According to the documentation : write(file, encoding="us-ascii", xml_declaration=None, method="xml") Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US

ElementTree's iter() equivalent in Python2.6

爷,独闯天下 提交于 2019-12-01 03:04:27
I have this code with ElementTree that works well with Python 2.7. I needed to get all the nodes with the name "A" under "X/Y" node. from xml.etree.ElementTree import ElementTree verboseNode = topNode.find("X/Y") nodes = list(verboseNode.iter("A")) However, when I tried to run it with Python 2.6, I got this error. ionCalculateSkewConstraint.py", line 303, in getNodesWithAttribute nodes = list(startNode.iter(nodeName)) AttributeError: _ElementInterface instance has no attribute 'iter' It looks like that Python 2.6 ElementTree's node doesn't have the iter(). How can I implement the iter() with