elementtree

Python ElementTree default namespace?

可紊 提交于 2019-11-30 12:49:46
问题 Is there a way to define the default/unprefixed namespace in python ElementTree? This doesn't seem to work... ns = {"":"http://maven.apache.org/POM/4.0.0"} pom = xml.etree.ElementTree.parse("pom.xml") print(pom.findall("version", ns)) Nor does this: ns = {None:"http://maven.apache.org/POM/4.0.0"} pom = xml.etree.ElementTree.parse("pom.xml") print(pom.findall("version", ns)) This does, but then I have to prefix every element: ns = {"mvn":"http://maven.apache.org/POM/4.0.0"} pom = xml.etree

Parse and count numeric only xml text including e-00 or e+01

别来无恙 提交于 2019-11-30 09:57:12
问题 I am a python newbie. I am trying to parse through an xml file and count all text inputs that are all numeric including approximated values using e- or e+. E.g. Given the psuedo code below (jerry.xml), <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <language>english</language> <currency>1.21$/kg</currency> <gdppc>141100</gdppc> <gdpnp>2.304e+0150</gdpnp> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name=

How do I set attributes for an XML element with Python?

淺唱寂寞╮ 提交于 2019-11-30 09:44:55
I am using ElementTree to build an XML file. When I try to set an element's attribute with ET.SubElement().__setattr__() , I get the error AttributeError: __setattr__ . import xml.etree.cElementTree as ET summary = open(Summary.xml, 'w') root = ET.Element('Summary') ET.SubElement(root, 'TextSummary') ET.SubElement(root,'TextSummary').__setattr__('Status','Completed') # Error occurs here tree = ET.ElementTree(root) tree.write(summary) summary.close() After code execution, my XML should resemble the following: <Summary> <TextSummary Status = 'Completed'/> </Summary> How do I add attributes to an

Write xml utf-8 file with utf-8 data with ElementTree

落爺英雄遲暮 提交于 2019-11-30 08:55:45
I'm trying to write an xml file with utf-8 encoded data using ElementTree like this: #!/usr/bin/python # -*- coding: utf-8 -*- import xml.etree.ElementTree as ET import codecs testtag = ET.Element('unicodetag') testtag.text = u'Töreboda' #The o is really ö (o with two dots over). No idea why SO dont display this expfile = codecs.open('testunicode.xml',"w","utf-8-sig") ET.ElementTree(testtag).write(expfile,encoding="UTF-8",xml_declaration=True) expfile.close() This blows up with the error Traceback (most recent call last): File "unicodetest.py", line 10, in <module> ET.ElementTree(testtag)

Search and remove element with elementTree in Python

独自空忆成欢 提交于 2019-11-30 08:11:10
I have an XML document in which I want to search for some elements and if they match some criteria I would like to delete them However, I cannot seem to be able to access the parent of the element so that I can delete it file = open('test.xml', "r") elem = ElementTree.parse(file) namespace = "{http://somens}" props = elem.findall('.//{0}prop'.format(namespace)) for prop in props: type = prop.attrib.get('type', None) if type == 'json': value = json.loads(prop.attrib['value']) if value['name'] == 'Page1.Button1': #here I need to access the parent of prop # in order to delete the prop Is there a

How to keep comments while parsing XML using Python / ElementTree

余生长醉 提交于 2019-11-30 07:13:01
问题 Currently using Python 2.4.3, and not allowed to upgrade I want to change the values of a given attribute in one or more tags, together with XML-comments in the updated file. I have managed to create a Python script that takes a XML-file as argument, and for each tag specified changes an attribute, as shown below def update(file, state): global Etree try: from elementtree import ElementTree print '*** using ElementTree' except ImportError, e: print '***' print '*** Error: Must install either

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

心不动则不痛 提交于 2019-11-30 07:03:21
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> 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.etree.ElementTree as xml >>> s= '<p>blah <b>bleh</b> blih</p>\n' >>> t=xml.fromstring(s) >>> "".join( [ t.text

Python: ElementTree, get the namespace string of an Element

和自甴很熟 提交于 2019-11-30 06:26:45
This XML file is named example.xml : <?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>14.0.0</modelVersion> <groupId>.com.foobar.flubber</groupId> <artifactId>uberportalconf</artifactId> <version>13-SNAPSHOT</version> <packaging>pom</packaging> <name>Environment for UberPortalConf</name> <description>This is the description</description> <properties> <birduberportal.version>11</birduberportal.version>

How to find XML Elements via XPath in Python in a namespace-agnostic way?

瘦欲@ 提交于 2019-11-30 06:26:20
问题 since I had this annoying issue for the 2nd time, I thought that asking would help. Sometimes I have to get Elements from XML documents, but the ways to do this are awkward. I’d like to know a python library that does what I want, a elegant way to formulate my XPaths, a way to register the namespaces in prefixes automatically or a hidden preference in the builtin XML implementations or in lxml to strip namespaces completely. Clarification follows unless you already know what I want :) Example

Elementtree setting attribute order

无人久伴 提交于 2019-11-30 05:28:48
问题 I am trying to write a python script to standardise generic XML files, used to configure websites and website forms. However to do this I would like to either maintain the original attribute ordering of the elements, or even better be able to rearrange them in a pre-defined way. Currently most xml parsers I have tried re-write the attribute order to be alpha-numeric. As these XML files are human read/written and maintained, this isn't too useful. For example a generic element may look like