xml.etree

python alexa result parsing with lxml.etree

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 04:26:33
问题 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<

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

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

对着背影说爱祢 提交于 2019-12-25 11:06:11
问题 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

How to delete duplicated elements in XML file

拈花ヽ惹草 提交于 2019-12-24 12:02:02
问题 Here is my XML file: it contains a duplicated element <houseNum>0</houseNum> . <?xml version="1.0" encoding="utf-8"?> <ArrayOfHouse> <XmlForm> <houseNum>0</houseNum> <plan1> <coord> <X> 1.2 </X> <Y> 2.1 </Y> <Z> 3.0 </Z> </coord> <color> <R> 255 </R> <G> 0 </G> <B> 0 </B> </color> </plan1> <plan2> <coord> <X> 21.2 </X> <Y> 22.1 </Y> <Z> 31.0 </Z> </coord> <color> <R> 255 </R> <G> 0 </G> <B> 0 </B> </color> </plan2> </XmlForm> <XmlForm> <houseNum>0</houseNum> <plan1> <coord> <X> 1.2 </X> <Y> 2

Parse xml from file using etree works when reading string, but not a file

a 夏天 提交于 2019-12-20 04:12:09
问题 I am a relative newby to Python and SO. I have an xml file from which I need to extract information. I've been struggling with this for several days, but I think I finally found something that will extract the information properly. Now I'm having troubles getting the right output. Here is my code: from xml import etree node = etree.fromstring('<dataObject><identifier>5e1882d882ec530069d6d29e28944396</identifier><description>This is a paragraph about a shark.</description></dataObject>')

How to remove a node inside an iterator in python xml.etree.ElementTree

你说的曾经没有我的故事 提交于 2019-12-19 09:59:52
问题 How to remove the current node, while iterating through all nodes from root by getiterator() function? import xml.etree.ElementTree as ET tree = ET.parse('file.xml') root = tree.getroot() for node in root.getiterator(): #if some condition: #remove(node) 回答1: You can't remove nodes without knowing the parent, but the xml.etree package doesn't give you any way to access a parent from a given node. The only way around this is matching the parent node instead: for node in root.iter(): if some

Updating XML elements and attribute values using Python etree

狂风中的少年 提交于 2019-12-18 13:25:14
问题 I'm trying to use Python 2.7's ElementTree library to parse an XML file, then replace specific element attributes with test data, then save this as a unique XML file. My idea for a solution was to (1) source new data from a CSV file by reading a file to a string, (2) slice the string at certain delimiter marks, (3) append to a list, and then (4) use ElementTree to update/delete/replace the attribute with a specific value from the list. I've looked in the ElementTree documentation & saw the

parsing XML configuration file using Etree in python

偶尔善良 提交于 2019-12-12 19:36:30
问题 Please help me parse a configuration file of the below prototype using lxml etree. I tried with for event, element with tostring. Unfortunately I don't need the text, but the XML between <template name> <config> </template> for a given attribute. I started with this code, but get a key error while searching for the attribute since it scans from start config_tree = etree.iterparse(token_template_file) for event, element in config_tree: if element.attrib['name']=="ad auth": print ("attrib

etree SubElement attribute name class fails

安稳与你 提交于 2019-12-11 20:43:06
问题 I need to force python(2.7.5) to use the word class in building a xml file properties = ET.SubElement(head, "properties", class="model.View$PropertyList") ^ SyntaxError: invalid syntax I tried '' or "" properties = ET.SubElement(head, "properties", "class"="hudson.model.View$PropertyList") SyntaxError: keyword can't be an expression If I change it to another name (foo), it builds the xml: <properties foo="hudson.model.View$PropertyList" /> 回答1: You can use attrib={} syntax: head = ET.Element(

How do I access text between tags with xml.etree.ElementTree

荒凉一梦 提交于 2019-12-11 05:17:27
问题 I am trying to extract the text value between two tags of an XML document with xml.etree.ElementTree . In the following example, that would be the values text two and text three . I can extract only text one . How would I find the other texts from the <c> tag? import xml.etree.ElementTree as ET root = ET.fromstring( "<foo><c>text one<sub>ttt</sub>text two<sub>uuu</sub>text three</c></foo>") print root[0].text # text one 回答1: Use itertext: >>> z <Element 'c' at 0x1030697d0> >>> for i in z