elementtree

How do I escape colons in an attribute name with Python's ElementTree?

£可爱£侵袭症+ 提交于 2019-12-23 15:26:37
问题 Background I am using ElementTree in Python version 2.6 to create an XML file (using data retrieved from a database). Code The following line of code is the problem area, as I keep getting a syntax error because of the colons within my attribute names. # Please ignore any errors the "^" characters would cause if they were # actually part of my code - just using them as placeholders. root = ET.Element("databaseConfiguration", xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", ^ xsi

Should memory usage increase when using ElementTree.iterparse() when clear()ing trees?

倖福魔咒の 提交于 2019-12-23 08:46:28
问题 import os import xml.etree.ElementTree as et for ev, el in et.iterparse(os.sys.stdin): el.clear() Running the above on the ODP structure RDF dump results in always increasing memory. Why is that? I understand ElementTree still builds a parse tree, albeit with the child nodes clear() ed. If that is the cause of this memory usage pattern, is there a way around it? 回答1: You are clear ing each element but references to them remain in the root document. So the individual elements still cannot be

Python read xml with related child elements

▼魔方 西西 提交于 2019-12-23 02:22:12
问题 I have a xml file with this structure: <?DOMParser ?> <logbook:LogBook xmlns:logbook="http://www/logbook/1.0" version="1.2"> <product> <serialNumber value="764000606"/> </product> <visits> <visit> <general> <startDateTime>2014-01-10T12:22:39.166Z</startDateTime> <endDateTime>2014-03-11T13:51:31.480Z</endDateTime> </general> <parts> <part number="03081" name="WSSA" index="0016"/> </parts> </visit> <visit> <general> <startDateTime>2013-01-10T12:22:39.166Z</startDateTime> <endDateTime>2013-03

Parsing : String to XML

 ̄綄美尐妖づ 提交于 2019-12-23 02:01:38
问题 my API is supposed to take a string and typecast it to XML format. But i am consistently getting this error : ParseError: mismatched tag: line 1, column 764 XML <?xml version="1.0" encoding="utf-8" ?> <MasterDetails IssuerId="5" Version="12.2"> <XMLRequest /> <BookingDetails Amount="768" Comment="Hotel Travel Purchase" CurrencyCode="INR" PurchaseType="Hotel" SupplierName="SomeHotel" CardAlias="C_ALIAS" ValidFor="-1D" CurrencyType="B" /> <CDFs> <CDF FieldName="Order Date" FieldValue="2015-01

Parsing : String to XML

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 02:01:23
问题 my API is supposed to take a string and typecast it to XML format. But i am consistently getting this error : ParseError: mismatched tag: line 1, column 764 XML <?xml version="1.0" encoding="utf-8" ?> <MasterDetails IssuerId="5" Version="12.2"> <XMLRequest /> <BookingDetails Amount="768" Comment="Hotel Travel Purchase" CurrencyCode="INR" PurchaseType="Hotel" SupplierName="SomeHotel" CardAlias="C_ALIAS" ValidFor="-1D" CurrencyType="B" /> <CDFs> <CDF FieldName="Order Date" FieldValue="2015-01

Using ElementTree to parse an XML string with a namespace

巧了我就是萌 提交于 2019-12-22 11:12:03
问题 I have Googled my pants off to no avail. What I am trying to do is very simple: I'd like to access the UniqueID value in the following XML contained in a string using ElementTree. from xml.etree.ElementTree import fromstring xml_string = """<ListObjectsResponse xmlns='http://www.example.com/dir/'> <Item> <UniqueID>abcdefghijklmnopqrstuvwxyz0123456789</UniqueID> </Item> </ListObjectsResponse>""" NS = "http://www.example.com/dir/" tree = fromstring(xml_string) I know that I should use the

How to parse a .txt file into .xml?

拟墨画扇 提交于 2019-12-22 10:27:24
问题 This is my txt file: In File Name: C:\Users\naqushab\desktop\files\File 1.m1 Out File Name: C:\Users\naqushab\desktop\files\Output\File 1.m2 In File Size: Low: 22636 High: 0 Total Process time: 1.859000 Out File Size: Low: 77619 High: 0 In File Name: C:\Users\naqushab\desktop\files\File 2.m1 Out File Name: C:\Users\naqushab\desktop\files\Output\File 2.m2 In File Size: Low: 20673 High: 0 Total Process time: 3.094000 Out File Size: Low: 94485 High: 0 In File Name: C:\Users\naqushab\desktop

Processing XML in Python with ElementTree

徘徊边缘 提交于 2019-12-22 09:09:47
问题 I have a problem with ElementTree.iter(). So I tried this example in this link : http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/ So here's what I've tried: import elementtree.ElementTree as ET tree = ET.parse('XML_file.xml') root = tree.getroot() for elem in tree.iter(): print elem.tag, elem.attrib And I get this error AttributeError: ElementTree instance has no attribute 'iter' Additional info: The version of my Python is 2.4 I separately installed

Node.toprettyxml() adds newlines to DOCTYPE in Python

ⅰ亾dé卋堺 提交于 2019-12-22 08:18:41
问题 When using prettify my DOCTYPE is broken into three lines. How can I keep it on one line? The "broken" output: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE smil PUBLIC '-//W3C//DTD SMIL 2.0//EN' 'http://www.w3.org/2001/SMIL20/SMIL20.dtd'> <smil xmlns="http://www.w3.org/2001/SMIL20/Language"> <head> <meta base="rtmp://cp23636.edgefcs.net/ondemand"/> </head> <body> <switch> <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_256.mp4" system-bitrate="336000"/> <video src="mp4:soundcheck/1

search/replace content of xml

拟墨画扇 提交于 2019-12-22 08:08:21
问题 I've been successful using xml.etree.ElementTree to parse an xml, search for content, then write this to a different xml. However, I just worked with text, inside of a singe tag. import os, sys, glob, xml.etree.ElementTree as ET path = r"G:\\63D RRC GIS Data\\metadata\\general\\2010_contract" for fn in os.listdir(path): filepaths = glob.glob(path + os.sep + fn + os.sep + "*overall.xml") for filepath in filepaths: (pa, filename) = os.path.split(filepath) ####use this section to grab element