elementtree

Python ElementTree parsing unbound prefix error

佐手、 提交于 2019-12-01 02:41:01
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.etree.ElementTree.ParseError: unbound prefix: line 2, column 0 Do I need to specify something in order to

ElementTree findall 'or' operator

ε祈祈猫儿з 提交于 2019-12-01 02:12:02
问题 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? 回答1: Since ElementTree from stdlib provides only

Get parent element after using find method (xml.etree.ElementTree)

拜拜、爱过 提交于 2019-12-01 01:47:31
问题 I am working with a huge xml-file and try to extract information from different elements. import xml.etree.ElementTree as ET tree = ET.parse('t.xml') root = tree.getroot() To find the elements I use the find method: elm = root.find('.//Element[@elmid="1234"]') From this I extract information and in addition I need information from the parent element. But elm.find('..') returns only None as documented here: https://docs.python.org/3/library/xml.etree.elementtree.html Now I use the folowing:

Using python ElementTree's itertree function and writing modified tree to output file

旧时模样 提交于 2019-12-01 00:53:21
问题 I need to parse a very large (~40GB) XML file, remove certain elements from it, and write the result to a new xml file. I've been trying to use iterparse from python's ElementTree, but I'm confused about how to modify the tree and then write the resulting tree into a new XML file. I've read the documentation on itertree but it hasn't cleared things up. Are there any simple ways to do this? Thank you! EDIT: Here's what I have so far. import xml.etree.ElementTree as ET import re date_pages = []

How do I append new data to existing XML using Python ElementTree?

雨燕双飞 提交于 2019-11-30 22:58:59
I am new to Python/ ElementTree . I have the following XML sample: <users> <user username="admin" fullname="admin" password="" uid="1000"/> <user username="user1" fullname="user1" password="" grant_admin_rights="yes"><group>my_group</group><group>group_2</group></user> </users> I would like to append the following to this existing XML: <user username="+username+" password="+password+"><group>+newgroup+</group></user> so my final output should be like this: <users> <user username="admin" fullname="admin" password="" uid="1000"/> <user username="user1" fullname="user1" password="" grant_admin

Elementtree setting attribute order

不羁的心 提交于 2019-11-30 21:21:09
I am trying to write a python script to standardise generic XML files, used to configure websites and website forms. However to do this I would like to either maintain the original attribute ordering of the elements, or even better be able to rearrange them in a pre-defined way. Currently most xml parsers I have tried re-write the attribute order to be alpha-numeric. As these XML files are human read/written and maintained, this isn't too useful. For example a generic element may look like this in the XML; <Question QuestionRef="XXXXX" DataType="Integer" Text="Question Text" Availability=

How to add an element to xml file by using elementtree

混江龙づ霸主 提交于 2019-11-30 20:20:30
I've a xml file, and I'm trying to add additional element to it. the xml has the next structure : <root> <OldNode/> </root> What I'm looking for is : <root> <OldNode/> <NewNode/> </root> but actually I'm getting next xml : <root> <OldNode/> </root> <root> <OldNode/> <NewNode/> </root> My code looks like that : file = open("/tmp/" + executionID +".xml", 'a') xmlRoot = xml.parse("/tmp/" + executionID +".xml").getroot() child = xml.Element("NewNode") xmlRoot.append(child) xml.ElementTree(root).write(file) file.close() Thanks. You opened the file for appending, which adds data to the end. Open the

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

大城市里の小女人 提交于 2019-11-30 19:36:10
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</CRP> <ESR/> <Joints> <DAS_PROFILE>28/28</DAS_PROFILE> <SWOL28>15</SWOL28> <TEN28>20</TEN28> </Joints>

“XML or text declaration not at start of entity: line 2, column 0” when calling ElementTree.parse

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 17:43:38
问题 ElementTree.parse() fails in the simple example below with the error xml.etree.ElementTree.ParseError: XML or text declaration not at start of entity: line 2, column 0 The XML looks valid and the code is simple, so what am I doing wrong? xmlExample = """ <?xml version="1.0"?> <data> stuff </data> """ import io source = io.StringIO(xmlExample) import xml.etree.ElementTree as ET tree = ET.parse(source) 回答1: You have a newline at the beginning of the XML string , remove it: xmlExample = """<?xml

ElementTree XPath - Select Element based on attribute

谁都会走 提交于 2019-11-30 16:47:34
I am having trouble using the attribute XPath Selector in ElementTree, which I should be able to do according to the Documentation Here's some sample code XML <root> <target name="1"> <a></a> <b></b> </target> <target name="2"> <a></a> <b></b> </target> </root> Python def parse(document): root = et.parse(document) for target in root.findall("//target[@name='a']"): print target._children I am receiving the following Exception: expected path separator ([) Florian Bösch The syntax you're trying to use is new in ElementTree 1.3 . Such version is shipped with Python 2.7 or higher. If you have