elementtree

Python XpathEvaluator without namespace

雨燕双飞 提交于 2019-12-10 09:44:01
问题 I need to write a dynamic function that finds elements on a subtree of an ATOM xml document. To do so, I've written something like this: tree = etree.parse(xmlFileUrl) e = etree.XPathEvaluator(tree, namespaces={'def':'http://www.w3.org/2005/Atom'}) entries = e('//def:entry') for entry in entries: mypath = tree.getpath(entry) + "/category" category = e(mypath) The code above fails to find category. The reason is that getpath returns an XPath without namespaces, whereas the XPathEvaluator e()

Iterate over both text and elements in lxml etree

随声附和 提交于 2019-12-10 03:09:02
问题 Suppose I have the following XML document: <species> Mammals: <dog/> <cat/> Reptiles: <snake/> <turtle/> Birds: <seagull/> <owl/> </species> Then I get the species element like this: import lxml.etree doc = lxml.etree.fromstring(xml) species = doc.xpath('/species')[0] Now I would like to print a list of animals grouped by species. How could I do it using ElementTree API? 回答1: If you enumerate all of the nodes, you'll see a text node with the class followed by element nodes with the species: >

Insert a node for an element in XML with Python/ElementTree

旧巷老猫 提交于 2019-12-09 16:04:20
问题 I need to traverse the XML tree to add sub element when the value is less than 5. For example, this XML can be modified into <?xml version="1.0" encoding="UTF-8"?> <A value="45"> <B value="30"> <C value="10"/> <C value ="20"/> </B> <B value="15"> <C value = "5" /> <C value = "10" /> </B> </A> this XML. <?xml version="1.0" encoding="UTF-8"?> <A value="45"> <B value="30"> <C value="10"/> <C value ="20"/> </B> <B value="15"> <C value = "5"><D name="error"/></C> <C value = "10" /> </B> </A> How

Python ElementTree: Parsing a string and getting ElementTree instance

a 夏天 提交于 2019-12-09 05:06:13
问题 I have a string containing XML data that is returned from an http request. I am using ElementTree to parse the data, and I want to then search recursively for an element. According to this question, I can only search recursively with result.findall() if result is of type ElementTree rather than type Element . Now xml.etree.ElementTree.fromstring() , used to parse the string, returns an Element object, while xml.etree.ElementTree.parse() , used to parse a file , returns an ElementTree object.

using fromstring() with lxml prefixes

﹥>﹥吖頭↗ 提交于 2019-12-09 03:06:27
I have a variable ele. I'm trying to append a child node onto ele that contains a namespace prefix (called style) in its tag. ele seems to be aware of this prefix, as the line: print(ele.nsmap['style']) outputs urn:oasis:names:tc:opendocument:xmlns:style:1.0 But when I try to run ele.append(etree.fromstring('<style:style />')) I get the error lxml.etree.XMLSyntaxError: Namespace prefix style on style is not defined What am I missing here? etree.fromstring('<style:style />') throws an error because <style:style /> is a small XML document that is not namespace-well-formed . You have to declare

Python ElementTree parsing unbound prefix error

偶尔善良 提交于 2019-12-09 02:26:27
问题 I am learning ElementTree in python. Everything seems fine except when I try to parse the xml file with prefix: test.xml : <?xml version="1.0"?> <abc:data> <abc:country name="Liechtenstein" rank="1" year="2008"> </abc:country> <abc:country name="Singapore" rank="4" year="2011"> </abc:country> <abc:country name="Panama" rank="5" year="2011"> </abc:country> </abc:data> When I try to parse the xml: import xml.etree.ElementTree as ET tree = ET.parse('test.xml') I got the following error: xml

Python version 2.7: XML ElementTree: How to iterate through certain elements of a child element in order to find a match

守給你的承諾、 提交于 2019-12-09 00:32:21
问题 I'm a programming novice and only rarely use python so please bear with me as I try to explain what I am trying to do :) I have the following XML: <?xml version = "1.0" encoding = "utf-8"?> <Patients> <Patient> <PatientCharacteristics> <patientCode>3</patientCode> </PatientCharacteristics> <Visits> <Visit> <DAS> <CRP>14</CRP> <ESR/> <Joints> <DAS_PROFILE>28/28</DAS_PROFILE> <SWOL28>20</SWOL28> <TEN28>20</TEN28> </Joints> </DAS> <VisitDate>2010-02-17</VisitDate> </Visit> <Visit> <DAS> <CRP>10<

How do I set attributes for an XML element with Python?

和自甴很熟 提交于 2019-12-08 23:32:20
问题 I am using ElementTree to build an XML file. When I try to set an element's attribute with ET.SubElement().__setattr__() , I get the error AttributeError: __setattr__ . import xml.etree.cElementTree as ET summary = open(Summary.xml, 'w') root = ET.Element('Summary') ET.SubElement(root, 'TextSummary') ET.SubElement(root,'TextSummary').__setattr__('Status','Completed') # Error occurs here tree = ET.ElementTree(root) tree.write(summary) summary.close() After code execution, my XML should

ElementTree TypeError “write() argument must be str, not bytes” in Python3

此生再无相见时 提交于 2019-12-08 17:21:16
问题 Got a Problem with generating a .SVG File with Python3 and ElementTree. from xml.etree import ElementTree as et doc = et.Element('svg', width='480', height='360', version='1.1', xmlns='http://www.w3.org/2000/svg') #Doing things with et and doc f = open('sample.svg', 'w') f.write('<?xml version=\"1.0\" standalone=\"no\"?>\n') f.write('<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n') f.write('\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n') f.write(et.tostring(doc)) f.close() The

Parse all the xml files in a directory one by one using ElementTree

不问归期 提交于 2019-12-08 15:01:15
问题 I'm parsing XML in python by ElementTree import xml.etree.ElementTree as ET tree = ET.parse('try.xml') root = tree.getroot() I wish to parse all the 'xml' files in a given directory. The user should enter only the directory name and I should be able to loop through all the files in directory and parse them one by one. Can someone tell me the approach. I'm using Linux. 回答1: Just create a loop over os.listdir() : import os path = '/path/to/directory' for filename in os.listdir(path): if not