elementtree

Wildcard search at any nested depth using xml.etree.ElementTree

一个人想着一个人 提交于 2019-12-12 05:48:15
问题 I have a group of XML files which contain entries like <group name="XXX common string"> <value val="12" description="a dozen"> <text>one less than a baker's dozen</text> </value> <value val="13" description="a baker's dozen"> <text>One more than a dozen</text> </value> </group> <group name="YYY common string"> <value val="42" description="the answer"> <text>What do you get if you multiple 6 by 9?</text> </value> </group> Is there any simple way, using import xml.etree.ElementTree as ET and

How to write xml file with multiple root element using ElementTree in python

假如想象 提交于 2019-12-12 05:30:11
问题 I have python script and I have already written logic of writing xml file using xml.etree.cElementTree and the logic is look like below import xml.etree.cElementTree as ET root = ET.Element("root") for I in range(0,10): ET.SubElement(root, "field1").text = "some value1" ET.SubElement(root, "field2").text = "some vlaue2" tree = ET.ElementTree(root) tree.write("filename.xml") and it give output like <root> <field1>some value1</field1> <field2>some value2</field2> <field1>some value1</field1>

how to iterate through every element of a complicated XML tree

自闭症网瘾萝莉.ら 提交于 2019-12-12 05:18:59
问题 I have declared a root this way: root = ET.fromstring(xml_data) and let's say I have a XML code that looks like this <a> <b> <c> <d> <e> ... </e> </d> </c> </b> </a> I am not too sure how to go about iterating through each of these elements in a element tree. I've thought about doing : while (True) for child in root print child.tag but this ended up with this infinite output: a a a a a Just to give you an idea of what I am trying to do, I am passing two XML codes, one is a pretty large and

Add to origonal xml file from for loop in python

﹥>﹥吖頭↗ 提交于 2019-12-12 04:48:31
问题 I have a master xml file called vs_origonal_M.xml I want to add all types of a certain child <location> </location> <location> </location> . . . <location> </location> until all the files are looked at. I am doing this by first opening the directory, next I am making a list of all the files in the directory and checking to see if they are indeed xml files, then I am taking a certain child out. Then (Here's where I am stuck) I need to open the master file and insert this child right under the

python to merge certain XML tags from file1 to file 2

纵饮孤独 提交于 2019-12-12 04:38:11
问题 I have two xmls. XML1 and XML2. i would like to update XMl1 with the content of XML2. XML1 <vdu xmlns="test:file"> <lsm> <m-id>v1</m-id> <name>v1</name> <communication>bi</communication> <states> <s-name>stage1</s-name> <state> <s-type>defaultState</s-type> <s-func> <p-name>pkgname</p-name> <f-list> <f-name>funcNAME</f-name> <f-arg>{&}</f-arg> </f-list> </s-func> </state> <lib-path>libpath</lib-path> <e-list> <e-name>noEvent</e-name> <event> <nss>INC</nss> <nfs>INC</nfs> <actions> <p-name

How to get grandchild elements from Elementtree in python

只谈情不闲聊 提交于 2019-12-12 04:37:57
问题 Say I have an XML code like this one: <root> <a> <b> .... </b> <c> .... </c> <d> .... </d> </a> <d><c></c><a></a></d> </root> Is there a function to get the grandchildren elements given a certain child node? Foe example, in the XML code above, if I pass 'd', I would like it to return 'c' and 'a'. I tried getChildren(), but I guess this returns attributes, but not the children elements. I don't even have attributes btw. Thank you. 回答1: The root element is iterable: >>> import xml.etree

Merge two XML files by matching elements by attribute value

你离开我真会死。 提交于 2019-12-12 04:36:44
问题 I have two XML files that I'm trying to merge. I looked at other previous questions, but I don't feel like I can solve my problem from reading those. What I think makes my situation unique is that I have to find elements by attribute value and then merge to the opposite file. I have two files. One is an English translation catalog and the second is a Japanese translation catalog. Pleas see below. In the code below you'll see the XML has three elements which I will be merging children on -

Python elementtree find function reads Signature as empty (None)

你说的曾经没有我的故事 提交于 2019-12-12 04:35:59
问题 I'm trying to read Signature, SignatureValue and SignedInfo from signed XML in Python with ElementTree but it reads as None. Other xml attributes are read fine. How can I read Signature, SignatureValue and SignedInfo? Here's my code snippet: xml_file = open(settings.STATIC_ROOT + '/file/test.xml', 'rt').read() response_xml = xml_et.fromstring(xml_file.encode('utf-8')) print response_xml.find('Signature') # prints None print response_xml.find('SignatureValue') # prints None print response_xml

Parsing XML with invalid nodes

孤街浪徒 提交于 2019-12-12 04:28:22
问题 I have parsing too big XML. When a node fails I want to keep looping and doing stuff with remaining nodes. version 1 for event, element in etree.iterparse(file): if element.tag == "tag1": # Doing some stuff with the first version I get an exception: ParseError: not well-formed (invalid token): line 319851 So in order to process the remain nodes I have wrote a second version: version 2 xml_parser = etree.iterparse(file) while True: try: event, element = next(xml_parser) if element.tag == "tag1

Python replace XML content with Etree

我只是一个虾纸丫 提交于 2019-12-12 04:22:00
问题 I'd like to parse and compare 2 XML files with the Python Etree parser as follows: I have 2 XML files with loads of data. One is in English (the source file), the other one the corresponding French translation (the target file). E.g.: source file: <AB> <CD/> <EF> <GH> <id>123</id> <IJ>xyz</IJ> <KL>DOG</KL> <MN>dogs/dog</MN> some more tags and info on same level <metadata> <entry> <cl>Translation</cl> <cl>English:dog/dogs</cl> </entry> <entry> <string>blabla</string> <string>blabla</string> <