elementtree

Python: xPath not available in ElementTree

孤人 提交于 2021-02-19 04:45:35
问题 I am trying to parse iTunes Playlist by using iterparse() of ElementTree but getting following error: AttributeError: 'Element' object has no attribute 'xpath' Code is given below: import xml.etree.ElementTree as ET context = ET.iterparse(file,events=("start", "end")) # turn it into an iterator context = iter(context) # get the root element event, root = context.next() for event, elem in context: z = elem.xpath(".//key") elem.clear() root.clear() print z What I am doing wrong? File is too big

Python ElementTree: find element by its child's text using XPath

China☆狼群 提交于 2021-02-19 04:19:39
问题 I'm trying to locate an element that has certain text value in one of its child. For example, <peers> <peer> <offset>1</offset> <tag>TRUE</tag> </peer> <peer> <offset>2</offset> <tag>FALSE</tag> </peer> </peers> from this XML document I would like to directly locate tag in a peer element whose offset value is 1. So for that purpose I have a XPath expression as follows: ./peers/peer[offset='1']/tag however using such expression in ElementTree's Element.find() method fails and gives None rather

Python ElementTree: find element by its child's text using XPath

╄→尐↘猪︶ㄣ 提交于 2021-02-19 04:19:01
问题 I'm trying to locate an element that has certain text value in one of its child. For example, <peers> <peer> <offset>1</offset> <tag>TRUE</tag> </peer> <peer> <offset>2</offset> <tag>FALSE</tag> </peer> </peers> from this XML document I would like to directly locate tag in a peer element whose offset value is 1. So for that purpose I have a XPath expression as follows: ./peers/peer[offset='1']/tag however using such expression in ElementTree's Element.find() method fails and gives None rather

Python ElementTree: find element by its child's text using XPath

自作多情 提交于 2021-02-19 04:18:23
问题 I'm trying to locate an element that has certain text value in one of its child. For example, <peers> <peer> <offset>1</offset> <tag>TRUE</tag> </peer> <peer> <offset>2</offset> <tag>FALSE</tag> </peer> </peers> from this XML document I would like to directly locate tag in a peer element whose offset value is 1. So for that purpose I have a XPath expression as follows: ./peers/peer[offset='1']/tag however using such expression in ElementTree's Element.find() method fails and gives None rather

How to have an XML tag start with a number?

假装没事ソ 提交于 2021-02-16 19:13:31
问题 I'm using ElementTree API to read and write to an XML document. When I try to add a tag that starts with a number, the XML file is no longer valid. Using import xml.etree.cElementTree as ET , I am successfully able to create the XML document, but when I try to read in the XML file again, I get a ParseError. For my purposes, it does not matter if the XML document is not well formed. I just need to be able to start a tag with a number. Any idea how to do this? This is what I have tried: from

Writing a Python tool to convert XML to Python?

大城市里の小女人 提交于 2021-02-10 16:24:34
问题 I've been asked to look in to the possibility of converting XML to Python, so that the XML can be phased out of the current process & they'd be replaced with new python scripts. Currently the XML files are used by python & ASP and look essentially like this; <?xml version="1.0" encoding="UTF-8"?> <script> <stage id="stage1" nextStage="stage2"> <initialise> <variable id="year" value="2012" type="Integer"/> <executeMethod class="engineclass_date" method="getTodayAsString"> <arguments> <variable

How to write unescaped string to a XML element with ElementTree?

点点圈 提交于 2021-02-08 08:23:13
问题 I have a string variable contents with following value: <ph type="0" x="1"></ph> I try to write it to a XML element as follows: elemen_ref.text = contents After I write XML tree to a file and check it with Notepad++, I see following value written to the XML element: <ph type="0" x="1"></ph> How do I write unescaped string? Please note that this value is copied from another XML element which remains intact after writing tree to a file, so the issue is with assigning value to a text attribute.

How to write unescaped string to a XML element with ElementTree?

微笑、不失礼 提交于 2021-02-08 08:22:23
问题 I have a string variable contents with following value: <ph type="0" x="1"></ph> I try to write it to a XML element as follows: elemen_ref.text = contents After I write XML tree to a file and check it with Notepad++, I see following value written to the XML element: <ph type="0" x="1"></ph> How do I write unescaped string? Please note that this value is copied from another XML element which remains intact after writing tree to a file, so the issue is with assigning value to a text attribute.

Loop through XML in Python

瘦欲@ 提交于 2021-02-05 07:57:25
问题 My data set is as following: <?xml version="1.0" encoding="UTF-8"?> <depts xmlns="http://SOMELINK" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" date="2021-01-15"> <dept dept_id="00001" col_two="00001value" col_three="00001false" name = "some_name"> <owners> <currentowner col_four="00001value" col_five="00001value" col_six="00001false" name = "some_name"> <addr col_seven="00001value" col_eight="00001value" col_nine="00001false"/> <

How to read commented text from XML file in python

China☆狼群 提交于 2021-02-04 08:33:10
问题 I am able to read the xml file in a using 'import xml.etree.ElementTree as et'. But my problem is to read the commented text given in the data file, how to read this: For example in the below xml, I want to read BaseVehicle is 1997 Cadillac Catera <App action="A" id="1"> <BaseVehicle id="8559"/> <!-- 1997 Cadillac Catera --> <Qty>1</Qty> <PartType id="4472"/> <!-- Electrical/Headlight/Switch --> <Part>SW1406</Part> </App> 回答1: The standard behaviour of ElementTree is to ignore comments.