elementtree

Python ElementTree find() not matching within kml file

雨燕双飞 提交于 2019-12-04 17:20:25
I'm trying to find an element from a kml file using element trees as follows: from xml.etree.ElementTree import ElementTree tree = ElementTree() tree.parse("history-03-02-2012.kml") p = tree.find(".//name") A sufficient subset of the file to demonstrate the problem follows: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>Location history from 03/03/2012 to 03/10/2012</name> </Document> </kml> A "name" element exists; why does the search come back empty? The name element you're trying to match is actually within the KML namespace, but you

Selecting element by ID, in .svg xml

不问归期 提交于 2019-12-04 15:10:12
I generate a .svg image (which is a xml file) using Inkscape. I set a node's ID to 'mount-arm-r'. I want to read the attributes 'x', 'y', from that element. I can't seem to select the rect elements. python: I tried XPath, or manually one depth at a time. I think I want: def parse_xml(): tree = ElementTree() tree.parse("torso-human.svg") for node in tree.findall('.//rect'): print 'n=', node xml: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Created with Inkscape (http://www.inkscape.org/) --> <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#

Pylint Error Message: “E1101: Module 'lxml.etree' has no 'strip_tags' member'”

天涯浪子 提交于 2019-12-04 11:12:05
问题 I am experimenting with lxml and python for the first time for a personal project, and I am attempting to strip tags from a bit of source code using etree.strip_tags(). For some reason, I keep getting the error message: "E1101: Module 'lxml.etree' has no 'strip_tags' member'". I'm not sure why this is happening. Here's the relevant portion of my code: from lxml import etree ... DOC = etree.strip_tags(DOC_URL, 'html') print DOC Any ideas? Thanks. 回答1: The reason for this is that pylint by

python xml.etree.ElementTree append to subelement

我的未来我决定 提交于 2019-12-04 09:30:48
问题 I am trying to use xml.etree.ElementTree to parse a xml file, find a specific tag, append a child to that tag, append another child to the newly created tag and add text to the latter child. My XML: <root> <a> <b> <c>text1</c> </b> <b> <c>text2</c> </b> </a> </root> Desired XML: <root> <a> <b> <c>text1</c> </b> <b> <c>text2</c> </b> <b> <c>text3</c> </b> </a> </root> Current code: import xml.etree.ElementTree as ET tree = ET.parse('test.xml') root = tree.getroot() for x in root.iter(): if (x

Get text from mixed element xml tags with ElementTree

你。 提交于 2019-12-04 07:27:21
问题 I'm using ElementTree to parse an XML document that I have. I am getting the text from the u tags. Some of them have mixed content that I need to filter out or keep as text. Two examples that I have are: <u> <vocal type="filler"> <desc>eh</desc> </vocal>¿Sí? </u> <u>Pues... <vocal type="non-ling"> <desc>laugh</desc> </vocal>A mí no me suena. </u> I want to get the text within the vocal tag if it's type is filler but not if it's type is non-ling . If I iterate through the children of u ,

How do you parse nested XML tags with python?

隐身守侯 提交于 2019-12-04 07:04:21
Please excuse me if I'm using the wrong terminology, but here's what I'm trying to accomplish. I'm trying to pull attribute and text information from nested tags in such as alias, payment, amount, and etc... However my example code block is only able to pull info from and not anything from the subelements in . How do I go about using elementtree to try and get to the subelements of my subelements? Once please excuse my terminology if I'm using it incorrectly: ** Example XML block: ** <root> <host name="comp1"> <alias>smith_laptop</alias> <ipAddr>102.168.1.1</ipAddr> <owner>Mr_Smith</owner>

root of the xml file is giving as NONE why?

荒凉一梦 提交于 2019-12-04 06:47:38
问题 from elementtree import ElementTree as ET tree= ET.parse(r'N:\myinternwork\files xml of bus systems\testonieeebus.xml','r') root= tree.getroot() print(root) now the error is in output as it is giving none <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <author>Giada De Laurentiis</author> </book> </bookstore> 回答1: The following code is enough. You don't need to open the file at beginning. ET.parse will do it for you if you provide the correct path. In your code

Exported xml isn't well formatted but appears in one line?

泪湿孤枕 提交于 2019-12-04 06:42:13
问题 Here is the code but the exported xml appears badly formatted. import xml.etree.ElementTree as ET import os sampleXML = """<?xml version="1.0" encoding="ASCII"?> <Metadata version="1.0"> <CODE_OK>510</CODE_OK> <DeliveryDate>13/08/2018</DeliveryDate> </Metadata> """ tree = ET.ElementTree(ET.fromstring(sampleXML)) for folder in os.listdir("YourPath"): #Iterate the dir tree.find("CODE_OK").text = folder #Update dir name in XML tree.write(open(os.path.join(r"Path", folder, "newxml.xml"), "wb"))

Python ElementTree won't convert non-breaking spaces when using UTF-8 for output

…衆ロ難τιáo~ 提交于 2019-12-04 03:13:33
I'm trying to parse, manipulate, and output HTML using Python's ElementTree: import sys from cStringIO import StringIO from xml.etree import ElementTree as ET from htmlentitydefs import entitydefs source = StringIO("""<html> <body> <p>Less than <</p> <p>Non-breaking space  </p> </body> </html>""") parser = ET.XMLParser() parser.parser.UseForeignDTD(True) parser.entity.update(entitydefs) etree = ET.ElementTree() tree = etree.parse(source, parser=parser) for p in tree.findall('.//p'): print ET.tostring(p, encoding='UTF-8') When I run this using Python 2.7 on Mac OS X 10.6, I get: <p>Less than <<

How to set ElementTree Element text field in the constructor

自闭症网瘾萝莉.ら 提交于 2019-12-04 02:50:12
问题 How do I set the text field of of ElementTree Element from its constructor? Or, in the code below, why is the second print of root.text None? import xml.etree.ElementTree as ET root = ET.fromstring("<period units='months'>6</period>") ET.dump(root) print root.text root=ET.Element('period', {'units': 'months'}, text='6') ET.dump(root) print root.text root=ET.Element('period', {'units': 'months'}) root.text = '6' ET.dump(root) print root.text Here the output: <period units="months">6</period> 6