elementtree

Setting an attribute value in XML file using ElementTree

十年热恋 提交于 2020-02-03 05:38:10
问题 I need to change the value of an attribute named approved-by in an xml file from 'no' to 'yes'. Here is my xml file: <?xml version="1.0" encoding="UTF-8" ?> <!--Arbortext, Inc., 1988-2008, v.4002--> <!DOCTYPE doc PUBLIC "-//MYCOMPANY//DTD XSEIF 1/FAD 110 05 R5//EN" "XSEIF_R5.dtd"> <doc version="XSEIF R5" xmlns="urn:x-mycompany:r2:reg-doc:1551-fad.110.05:en:*"> <meta-data> <?Pub Dtl?> <confidentiality class="mycompany-internal" /> <doc-name>INSTRUCTIONS</doc-name> <doc-id> <doc-no type=

Retaining empty elements when parsing with ElementTree

梦想的初衷 提交于 2020-01-26 04:57:19
问题 Using Python 3.4 and ElementTree, I'm trying to add a sub-element to an xml file, keeping the xml file (written in UTF-16) otherwise exactly the same. My code: new = new_XML_file.xml tree = ET.parse(new) root = tree.getroot() new_element = ET.SubElement(root, 'RENAMED_SOUND_FILE') new_element.text=new.split('\\')[num][:-4]+'.wav' tree.write(fake_path++new.split('\\')[num], encoding='utf-16', xml_declaration=True) The problem I'm having is that empty elements are being changed in this process.

ElementTree Remove Element

≡放荡痞女 提交于 2020-01-23 12:22:13
问题 Python noob here. Wondering what's the cleanest and best way to remove all the " profile " tags with updated attribute value of true . I have tried the following code but it's throwing: SyntaxError("cannot use absolute path on element") root.remove(root.findall("//Profile[@updated='true']")) XML: <parent> <child type="First"> <profile updated="true"> <other> </other> </profile> </child> <child type="Second"> <profile updated="true"> <other> </other> </profile> </child> <child type="Third">

ElementTree Remove Element

大城市里の小女人 提交于 2020-01-23 12:18:47
问题 Python noob here. Wondering what's the cleanest and best way to remove all the " profile " tags with updated attribute value of true . I have tried the following code but it's throwing: SyntaxError("cannot use absolute path on element") root.remove(root.findall("//Profile[@updated='true']")) XML: <parent> <child type="First"> <profile updated="true"> <other> </other> </profile> </child> <child type="Second"> <profile updated="true"> <other> </other> </profile> </child> <child type="Third">

Extracting specific xml tag value using python

依然范特西╮ 提交于 2020-01-23 02:47:06
问题 I have XML data which looks like this: <root> <results preview='0'> <meta> <fieldOrder> <field>title</field> <field>search</field> </fieldOrder> </meta> <messages> <msg type="DEBUG">msg1</msg> <msg type="DEBUG">msg2</msg> </messages> <result offset='0'> <field k='title'> <value> <text>text1</text> </value> </field> <field k='search'> <value> <text>text2</text> </value> </field> </result> </results> </root> I want to extract the tag value text2 from the tag k='search'>value>text . In my code,

Extract and save information of an xml file with python and parsing

穿精又带淫゛_ 提交于 2020-01-22 03:41:47
问题 Python novice here who has an XML structure which looks like this: <!-- ====================================================================== --> <person id="10007071"> <attributes> <attribute name="age" class="java.lang.Integer" >31</attribute> <attribute name="bikeAvailability" class="java.lang.String" >none</attribute> <attribute name="carAvailability" class="java.lang.String" >all</attribute> <attribute name="censusId" class="java.lang.Integer" >3676634</attribute> <attribute name=

Python - Element Tree is removing the XML declaration

限于喜欢 提交于 2020-01-21 06:37:18
问题 I'm writing some XML with element tree. I'm giving the code an empty template file that starts with the XML declaration: <?xml version= "1.0"?> when ET has finished making its changes and writes the completed XML its stripping out the declarion and starting with the root tag. How can I stop this? Write call: ET.ElementTree(root).write(noteFile) 回答1: According to the documentation: write(file, encoding="us-ascii", xml_declaration=None, method="xml") Writes the element tree to a file, as XML.

How do I append new data to existing XML using Python ElementTree?

孤人 提交于 2020-01-21 05:21:05
问题 I am new to Python/ElementTree. I have the following XML sample: <users> <user username="admin" fullname="admin" password="" uid="1000"/> <user username="user1" fullname="user1" password="" grant_admin_rights="yes"><group>my_group</group><group>group_2</group></user> </users> I would like to append the following to this existing XML: <user username="+username+" password="+password+"><group>+newgroup+</group></user> so my final output should be like this: <users> <user username="admin"

Replace multiple texts with their corresponding texts in XML using Python - Part 2 -

你离开我真会死。 提交于 2020-01-17 05:10:07
问题 I would like to replace a text of aliasname element with a text from name if there is no corresponding key in the dictionary. here is the xml that I am working on now. - <esri:Workspace xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"> - <WorkspaceDefinition xsi:type="esri:WorkspaceDefinition"> <WorkspaceType>esriLocalDatabaseWorkspace</WorkspaceType> <Domains xsi:type="esri:ArrayOfDomain" />

I trouble in how do parse multiple xml file and process it as dataframe in Python

守給你的承諾、 提交于 2020-01-16 14:11:43
问题 I want parse multi xml file into dataframe. There are same xpath. I have used element tree and os Python library.It can parse all the files, but it print out empty dataframe. However if code without multiple file, it can work properly. mypath = r'C:\Users\testFile' files = [path.join(mypath, f) for f in listdir(mypath) if f.endswith('.xml')] for file in files: xtree = et.parse(file) xroot = xtree.getroot() df_cols=['value'] out_xml=pd.DataFrame(columns=df_cols) for node in xroot.findall(r'.