elementtree

search/replace content of xml

核能气质少年 提交于 2019-12-05 12:14:48
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 text from old, archived metadata files; this text then gets put into current, working .xml### root = ET

Element Tree: How to parse subElements of child nodes

陌路散爱 提交于 2019-12-05 12:07:15
I have an XML tree, which I'd like to parse using Elementtree. My XML looks something like <?xml version="1.0" encoding="UTF-8"?> <GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents"> <Ack>Success</Ack> <Version>857</Version> <Build>E857_INTL_APIXO_16643800_R1</Build> <PaginationResult> <TotalNumberOfPages>1</TotalNumberOfPages> <TotalNumberOfEntries>2</TotalNumberOfEntries> </PaginationResult> <HasMoreOrders>false</HasMoreOrders> <OrderArray> <Order> <OrderID>221362908003-1324471823012</OrderID> <CheckoutStatus> <eBayPaymentStatus>NoPaymentFailure</eBayPaymentStatus> <LastModifiedTime

Processing XML in Python with ElementTree

半世苍凉 提交于 2019-12-05 11:49:32
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 elementtree. Other examples in the link that I provide is working in my Python installed. Only the ElementTree

elementtree register namespace error

半世苍凉 提交于 2019-12-05 05:38:29
I tried to register namespace with this: ET.register_namespace("inv", "http://www.stormware.cz/schema/version_2/invoice.xsd") but it doesn't work: Traceback (most recent call last): File "C:\tutorial\temp_xml2.py", line 34, in module> for listInvoice in root.findall('inv:invoiceHeader'): File "C:\Python27\LIB\xml\etree\ElementTree.py", line 390, in findall return ElementPath.findall(self, path, namespaces) File "C:\Python27\LIB\xml\etree\ElementPath.py", line 293, in findall return list(iterfind(elem, path, namespaces)) File "C:\Python27\LIB\xml\etree\ElementPath.py", line 259, in iterfind

Iterate over both text and elements in lxml etree

偶尔善良 提交于 2019-12-05 03:06:09
Suppose I have the following XML document: <species> Mammals: <dog/> <cat/> Reptiles: <snake/> <turtle/> Birds: <seagull/> <owl/> </species> Then I get the species element like this: import lxml.etree doc = lxml.etree.fromstring(xml) species = doc.xpath('/species')[0] Now I would like to print a list of animals grouped by species. How could I do it using ElementTree API? If you enumerate all of the nodes, you'll see a text node with the class followed by element nodes with the species: >>> for node in species.xpath("child::node()"): ... print type(node), node ... <class 'lxml.etree.

ElementTree element index look up

泄露秘密 提交于 2019-12-05 01:58:39
I'm using the xml.etree.ElementTree module to create an XML document with Python 3.1 from another structured document. What ElementTree function can I use that returns the index of an existing subelement? The getchildren method returns a list of sub-elements of an Element object. You could then use the built-in index method of a list. >>> import xml.etree.ElementTree as ET >>> root = ET.Element("html") >>> head = ET.SubElement(root, "head") >>> body = ET.SubElement(root, "body") >>> root.getchildren().index(body) 1 user2160906 import xml.etree.ElementTree as ET root=ET.Element('C:\Users

Access nested children in xml file parsed with ElementTree

我怕爱的太早我们不能终老 提交于 2019-12-05 00:11:32
I am new to xml parsing. This xml file has the following tree: FHRSEstablishment |--> Header | |--> ... |--> EstablishmentCollection | |--> EstablishmentDetail | | |-->... | |--> Scores | | |-->... |--> EstablishmentCollection | |--> EstablishmentDetail | | |-->... | |--> Scores | | |-->... but when I access it with ElementTree and look for the child tags and attributes, import xml.etree.ElementTree as ET import urllib2 tree = ET.parse( file=urllib2.urlopen('http://ratings.food.gov.uk/OpenDataFiles/FHRS408en-GB.xml' % i)) root = tree.getroot() for child in root: print child.tag, child.attrib I

Stripping python namespace attributes from an lxml.objectify.ObjectifiedElement [duplicate]

蓝咒 提交于 2019-12-04 22:29:37
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: When using lxml, can the XML be rendered without namespace attributes? How can I strip the python attributes from an lxml.objectify.ObjectifiedElement ? Example: In [1]: from lxml import etree, objectify In [2]: foo = objectify.Element("foo") In [3]: foo.bar = "hi" In [4]: foo.baz = 1 In [5]: foo.fritz = None In [6]: print etree.tostring(foo, pretty_print=True) <foo xmlns:py="http://codespeak.net/lxml/objectify

Write xml with a path and value

笑着哭i 提交于 2019-12-04 19:30:15
I have a list of paths and values, something like this: [ {'Path': 'Item/Info/Name', 'Value': 'Body HD'}, {'Path': 'Item/Genres/Genre', 'Value': 'Action'}, ] And I want to build out the full xml structure, which would be: <Item> <Info> <Name>Body HD</Name> </Info> <Genres> <Genre>Action</Genre> </Genres> </Item> Is there a way to do this with lxml ? Or how could I build a function to fill in the inferred paths? Padraic Cunningham You could do something like: l = [ {'Path': 'Item/Info/Name', 'Value': 'Body HD'}, {'Path': 'Item/Genres/Genre', 'Value': 'Action'}, ] import lxml.etree as et root

Why does ElementTree reject UTF-16 XML declarations with “encoding incorrect”?

左心房为你撑大大i 提交于 2019-12-04 17:49:48
问题 In Python 2.7, when passing a unicode string to ElementTree's fromstring() method that has encoding="UTF-16" in the XML declaration, I'm getting a ParseError saying that the encoding specified is incorrect: >>> from xml.etree import ElementTree >>> data = u'<?xml version="1.0" encoding="utf-16"?><root/>' >>> ElementTree.fromstring(data) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Program Files (x86)\Python 2.7\lib\xml\etree\ElementTree.py", line 1300, in