elementtree

nodejs elementtree npm xml parsing and merging

半世苍凉 提交于 2019-12-19 12:03:44
问题 I have a question on same module as I posted last time in below post. Seems like, it is quite difficult to deal with XML parsing. With elementTree having very mearge documentation. I have a below template_XML file: <?xml version="1.0" encoding="UTF-8"?> <Tailoring xmlns="http://checklists.nist.gov/xccdf/1.2" id="xccdf_org.open-scap_tailoring_example"> <status>incomplete</status> <version time="2013-01-15T16:00:00.000+02:00">1.0</version> <Profile id="PlaceHolder"> <title>Tailoring for py_test

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

Parsing XML with namespaces using ElementTree in Python

梦想的初衷 提交于 2019-12-19 04:46:08
问题 I have an xml, small part of it looks like this: <?xml version="1.0" ?> <i:insert xmlns:i="urn:com:xml:insert" xmlns="urn:com:xml:data"> <data> <image imageId="1"></image> <content>Content</content> </data> </i:insert> When i parse it using ElementTree and save it to a file i see following: <ns0:insert xmlns:ns0="urn:com:xml:insert" xmlns:ns1="urn:com:xml:data"> <ns1:data> <ns1:image imageId="1"></ns1:image> <ns1:content>Content</ns1:content> </ns1:data> </ns0:insert> Why does it change

Python element tree - extract text from element, stripping tags

假如想象 提交于 2019-12-19 02:05:32
问题 With ElementTree in Python, how can I extract all the text from a node, stripping any tags in that element and keeping only the text? For example, say I have the following: <tag> Some <a>example</a> text </tag> I want to return Some example text . How do I go about doing this? So far, the approaches I've taken have had fairly disastrous outcomes. 回答1: If you are running under Python 3.2+, you can use itertext . itertext creates a text iterator which loops over this element and all subelements

ElementTree XPath - Select Element based on attribute

只谈情不闲聊 提交于 2019-12-18 18:35:24
问题 I am having trouble using the attribute XPath Selector in ElementTree, which I should be able to do according to the Documentation Here's some sample code XML <root> <target name="1"> <a></a> <b></b> </target> <target name="2"> <a></a> <b></b> </target> </root> Python def parse(document): root = et.parse(document) for target in root.findall("//target[@name='a']"): print target._children I am receiving the following Exception: expected path separator ([) 回答1: The syntax you're trying to use is

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

How do I get the full XML or HTML content of an element using ElementTree?

浪子不回头ぞ 提交于 2019-12-18 12:27:12
问题 That is, all text and subtags, without the tag of an element itself? Having <p>blah <b>bleh</b> blih</p> I want blah <b>bleh</b> blih element.text returns "blah " and etree.tostring(element) returns: <p>blah <b>bleh</b> blih</p> 回答1: ElementTree works perfectly, you have to assemble the answer yourself. Something like this... "".join( [ "" if t.text is None else t.text ] + [ xml.tostring(e) for e in t.getchildren() ] ) Thanks to JV amd PEZ for pointing out the errors. Edit. >>> import xml

ElementTree SyntaxError: expected path separator ([)

两盒软妹~` 提交于 2019-12-18 09:06:09
问题 I've searched extensively for the past few days and can't seem to find what I'm looking for. I've written a script using Python 2.7.3 and ElementTree to parse an XML file and edit an attribute buried deep within the XML file. The script works fine. I had a meeting late last week with the customer who informed me the target platform will be CentOS. I thought, no problem. To test on the anticipated platform I created a CentOS VMWare client and much to my surprise my script crapped the bed,

get the namespaces from xml with python ElementTree

人盡茶涼 提交于 2019-12-18 07:09:31
问题 I use python 2.7 with the lib ElementTree. I can't use lxml lib. I need to get the namespaces in a string namespace_string . In order to fill my namespace dictionary. my xml: <?xml version="1.0" encoding="UTF-8"?> <AX_Bestandsdatenauszug xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0" xmlns:adv="http://www.adv-online.de/namespaces/adv/gid/6.0" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:ows=

get the namespaces from xml with python ElementTree

懵懂的女人 提交于 2019-12-18 07:08:10
问题 I use python 2.7 with the lib ElementTree. I can't use lxml lib. I need to get the namespaces in a string namespace_string . In order to fill my namespace dictionary. my xml: <?xml version="1.0" encoding="UTF-8"?> <AX_Bestandsdatenauszug xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0" xmlns:adv="http://www.adv-online.de/namespaces/adv/gid/6.0" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:ows=