elementtree

Iterating multiple (parent,child) nodes using Python ElementTree

 ̄綄美尐妖づ 提交于 2020-01-02 06:32:18
问题 The standard implementation of ElementTree for Python (2.6) does not provide pointers to parents from child nodes. Therefore, if parents are needed, it is suggested to loop over parents rather than children. Consider my xml is of the form: <Content> <Para>first</Para> <Table><Para>second</Para></Table> <Para>third</Para> </Content> The following finds all "Para" nodes without considering parents: (1) paras = [p for p in page.getiterator("Para")] This (adapted from effbot) stores the parent by

Iterating multiple (parent,child) nodes using Python ElementTree

£可爱£侵袭症+ 提交于 2020-01-02 06:32:12
问题 The standard implementation of ElementTree for Python (2.6) does not provide pointers to parents from child nodes. Therefore, if parents are needed, it is suggested to loop over parents rather than children. Consider my xml is of the form: <Content> <Para>first</Para> <Table><Para>second</Para></Table> <Para>third</Para> </Content> The following finds all "Para" nodes without considering parents: (1) paras = [p for p in page.getiterator("Para")] This (adapted from effbot) stores the parent by

Python ElementTree find() not matching within kml file

不打扰是莪最后的温柔 提交于 2020-01-01 18:16:53
问题 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

Error 'failed to load external entity' when using Python lxml

本小妞迷上赌 提交于 2020-01-01 07:31:06
问题 I'm trying to parse an XML document I retrieve from the web, but it crashes after parsing with this error: ': failed to load external entity "<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="GreenButtonDataStyleSheet.xslt"?> That is the second line in the XML that is downloaded. Is there a way to prevent the parser from trying to load the external entity, or another way to solve this? This is the code I have so far: import urllib2 import lxml.etree as etree file =

How do I remove a node in xml using ElementTree in Python?

你说的曾经没有我的故事 提交于 2019-12-29 07:02:39
问题 I've read the remove example here and the example here is not applicable to me. My xml file reads: <A> <B>some text</B> <B>other text</B> <B>more text</B> </A> What I want to do is to remove the second <B></B> from the xml. I do not know what text it holds. But I have the index of the <B></B> , say index = 1, which means I want to remove the second element (or node). I have a code like this: F = open('example.xml') self.tree = parse(F) self.root = self.tree.getroot() F.close() So in this case

Is there a way to get a line number from an ElementTree Element

北城以北 提交于 2019-12-28 13:22:49
问题 So I'm parsing some XML files using Python 3.2.1's cElementTree, and during the parsing I noticed that some of the tags were missing attribute information. I was wondering if there is any easy way of getting the line numbers of those Elements in the xml file. 回答1: Looking at the docs, I see no way to do this with cElementTree. However I've had luck with lxmls version of the XML implementation. Its supposed to be almost a drop in replacement, using libxml2. And elements have a sourceline

Is there a way to get a line number from an ElementTree Element

我的未来我决定 提交于 2019-12-28 13:21:07
问题 So I'm parsing some XML files using Python 3.2.1's cElementTree, and during the parsing I noticed that some of the tags were missing attribute information. I was wondering if there is any easy way of getting the line numbers of those Elements in the xml file. 回答1: Looking at the docs, I see no way to do this with cElementTree. However I've had luck with lxmls version of the XML implementation. Its supposed to be almost a drop in replacement, using libxml2. And elements have a sourceline

Parsing XML in Python using ElementTree example

别等时光非礼了梦想. 提交于 2019-12-28 04:59:47
问题 I'm having a hard time finding a good, basic example of how to parse XML in python using Element Tree. From what I can find, this appears to be the easiest library to use for parsing XML. Here is a sample of the XML I'm working with: <timeSeriesResponse> <queryInfo> <locationParam>01474500</locationParam> <variableParam>99988</variableParam> <timeParam> <beginDateTime>2009-09-24T15:15:55.271</beginDateTime> <endDateTime>2009-11-23T15:15:55.271</endDateTime> </timeParam> </queryInfo>

Extracting text after tag in Python's ElementTree

限于喜欢 提交于 2019-12-28 02:07:00
问题 Here is a part of XML: <item><img src="cat.jpg" /> Picture of a cat</item> Extracting the tag is easy. Just do: et = xml.etree.ElementTree.fromstring(our_xml_string) img = et.find('img') But how to get the text immediately after it ( Picture of a cat )? Doing the following returns a blank string: print et.text 回答1: Elements have a tail attribute -- so instead of element.text , you're asking for element.tail . >>> import lxml.etree >>> root = lxml.etree.fromstring('''<root><foo>bar</foo>baz<

Python: How to replace a character in a XML file with a new node?

ぐ巨炮叔叔 提交于 2019-12-25 11:07:02
问题 I want to replace all instances of semicolon ":" in my node below with a new node "<colon/>" as shown below. I want this: <shortName>Trigger:Digital Edge:Source</shortName> to become like this: <shortName>Trigger<colon/>Digital Edge<colon/>Source</shortName> I have already tried using search and replace string, but when I get the output all the "< >" change to &lt and &gt . Can anyone please suggest any techniques to do this. Thank You 回答1: The idea is to get the node text, split it by colon