xml.etree

do not collapse empty nodes in XML output

房东的猫 提交于 2019-12-11 02:18:16
问题 I'm using python's xml.etree.ElementTree to represent an XML document. I want to output it to text but I want to keep empty elements (elements with no children) expanded, instead of collapsed. E.g., I want this: <element></element> Instead of this: <element /> I'm currently using ElementTree.tostring , but I'm willing to use any other built-in python modules or functions to serialize the document, as long as I can pretty easily use an ElementTree object with it. FYI, the reason I want to keep

lxml.etree, element.text doesn't return the entire text from an element

℡╲_俬逩灬. 提交于 2019-12-03 16:18:42
问题 I scrapped some html via xpath, that I then converted into an etree. Something similar to this: <td> text1 <a> link </a> text2 </td> but when I call element.text, I only get text1 (It must be there, when I check my query in FireBug, the text of the elements is highlighted, both the text before and after the embedded anchor elements... 回答1: Use element.xpath("string()") or lxml.etree.tostring(element, method="text") - see the documentation. 回答2: As a public service to people out there who may

lxml.etree, element.text doesn't return the entire text from an element

放肆的年华 提交于 2019-12-03 05:25:08
I scrapped some html via xpath, that I then converted into an etree. Something similar to this: <td> text1 <a> link </a> text2 </td> but when I call element.text, I only get text1 (It must be there, when I check my query in FireBug, the text of the elements is highlighted, both the text before and after the embedded anchor elements... Use element.xpath("string()") or lxml.etree.tostring(element, method="text") - see the documentation . As a public service to people out there who may be as lazy as I am. Here's some code from above that you can run. from lxml import etree def get_text1(node):

python alexa result parsing with lxml.etree

半世苍凉 提交于 2019-12-02 06:19:35
I am using alexa api from aws but I find difficult in parse the result to get what I want alexa api return an object tree <type 'lxml.etree._ElementTree'> I use this code to print the tree from lxml import etree root = tree.getroot() print etree.tostring(root) I get xml below <aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11"><aws:OperationRequest><aws:RequestId>ccf3f263-ab76-ab63-db99-244666044e85</aws:RequestId></aws:OperationRequest><aws:UrlInfoResult><aws:Alexa> <aws:ContentData> <aws:DataUrl type=

How do I get properly escaped XML in python etree untouched?

泄露秘密 提交于 2019-12-01 19:27:40
I'm using python version 2.7.3. test.txt: <?xml version="1.0" encoding="UTF-8"?> <root> <test>The tag <StackOverflow> is good to bring up at parties.</test> </root> Result: >>> import xml.etree.ElementTree as ET >>> e = ET.parse('test.txt') >>> root = e.getroot() >>> print root.find('test').text The tag <StackOverflow> is good to bring up at parties. As you can see, the parser must have changed the < 's to < 's etc. What I'd like to see: The tag <StackOverflow> is good to bring up at parties. Untouched, raw text. Sometimes I really like it raw. Uncooked. I'd like to use this text as-is for

How do I get properly escaped XML in python etree untouched?

流过昼夜 提交于 2019-12-01 19:05:11
问题 I'm using python version 2.7.3. test.txt: <?xml version="1.0" encoding="UTF-8"?> <root> <test>The tag <StackOverflow> is good to bring up at parties.</test> </root> Result: >>> import xml.etree.ElementTree as ET >>> e = ET.parse('test.txt') >>> root = e.getroot() >>> print root.find('test').text The tag <StackOverflow> is good to bring up at parties. As you can see, the parser must have changed the < 's to < 's etc. What I'd like to see: The tag <StackOverflow> is good to bring up at parties.

parse .xml with prefix's on tags? xml.etree.ElementTree

烂漫一生 提交于 2019-11-30 21:55:03
I can read tags, except when there is a prefix. I'm not having luck searching SO for a previous question. I need to read media:content . I tried image = node.find("media:content") . Rss input: <channel> <title>Popular Photography in the last 1 week</title> <item> <title>foo</title> <media:category label="Miscellaneous">photography/misc</media:category> <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/> </item> <item> ... </item> </channel> I can read a sibling tag title . from xml.etree import ElementTree with open('cache1.rss', 'rt') as f: tree = ElementTree

parse .xml with prefix's on tags? xml.etree.ElementTree

旧街凉风 提交于 2019-11-30 17:32:58
问题 I can read tags, except when there is a prefix. I'm not having luck searching SO for a previous question. I need to read media:content . I tried image = node.find("media:content") . Rss input: <channel> <title>Popular Photography in the last 1 week</title> <item> <title>foo</title> <media:category label="Miscellaneous">photography/misc</media:category> <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/> </item> <item> ... </item> </channel> I can read a sibling

etree Clone Node

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 10:48:30
How to clone Element objects in Python xml.etree ? I'm trying to procedurally move and copy (then modify their attributes) nodes. You can just use copy.deepcopy() to make a copy of the element. (this will also work with lxml by the way). A different, and somewhat disturbing solution: new_element = lxml.etree.fromstring(lxml.etree.tostring(elem)) At least in Python 2.7 etree Element has a copy method: http://hg.python.org/cpython/file/2.7/Lib/xml/etree/ElementTree.py#l233 It is a shallow copy, but that is preferable in some cases. In my case I am duplicating some SVG Elements and adding a

etree Clone Node

[亡魂溺海] 提交于 2019-11-29 15:57:32
问题 How to clone Element objects in Python xml.etree ? I'm trying to procedurally move and copy (then modify their attributes) nodes. 回答1: You can just use copy.deepcopy() to make a copy of the element. (this will also work with lxml by the way). 回答2: A different, and somewhat disturbing solution: new_element = lxml.etree.fromstring(lxml.etree.tostring(elem)) 回答3: At least in Python 2.7 etree Element has a copy method: http://hg.python.org/cpython/file/2.7/Lib/xml/etree/ElementTree.py#l233 It is