elementtree

Python ElementTree default namespace?

为君一笑 提交于 2019-11-30 04:47:13
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.ElementTree.parse("pom.xml") print(pom.findall("mvn:version", ns)) Using Python 3.5 on OSX. EDIT: if the

Suppressing namespace prefixes in ElementTree 1.2

落花浮王杯 提交于 2019-11-30 04:41:40
In python 2.7 (with etree 1.3), I can suppress the XML prefixes on elements like this: Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import xml.etree.ElementTree as etree >>> etree.VERSION '1.3.0' >>> something = etree.Element('{http://some.namespace}token') >>> etree.tostring(something) '<ns0:token xmlns:ns0="http://some.namespace" />' >>> etree.register_namespace('', 'http://some.namespace') >>> etree.tostring(something) '<token xmlns=

How to add an element to xml file by using elementtree

点点圈 提交于 2019-11-30 04:18:35
问题 I've a xml file, and I'm trying to add additional element to it. the xml has the next structure : <root> <OldNode/> </root> What I'm looking for is : <root> <OldNode/> <NewNode/> </root> but actually I'm getting next xml : <root> <OldNode/> </root> <root> <OldNode/> <NewNode/> </root> My code looks like that : file = open("/tmp/" + executionID +".xml", 'a') xmlRoot = xml.parse("/tmp/" + executionID +".xml").getroot() child = xml.Element("NewNode") xmlRoot.append(child) xml.ElementTree(root)

Using Python's xml.etree to find element start and end character offsets

泄露秘密 提交于 2019-11-29 22:11:56
问题 I have XML data that looks like: <xml> The captial of <place pid="1">South Africa</place> is <place>Pretoria</place>. </xml> I would like to be able to extract: The XML elements as they're currently provided in etree. The full plain text of the document, between the start and end tags. The location within the plain text of each start element, as a character offset. (3) is the most important requirement right now; etree provides (1) fine. I cannot see any way to do (3) directly, but hoped that

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

喜你入骨 提交于 2019-11-29 17:38:21
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="Singapore"> <rank updated="yes">5</rank> <language>english</language> <currency>4.1$/kg</currency> <gdppc>59900

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

你。 提交于 2019-11-29 12:36:13
问题 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

get the namespaces from xml with python ElementTree

一个人想着一个人 提交于 2019-11-29 12:10:05
I use python 2.7 with the lib ElementTree. I can't use lxml lib. I need to get the namespaces in a string namespace_string . In order to fill my namespace dictionary. my xml: <?xml version="1.0" encoding="UTF-8"?> <AX_Bestandsdatenauszug xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0" xmlns:adv="http://www.adv-online.de/namespaces/adv/gid/6.0" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:ows="http://www.opengis.net/ows" xmlns:wfs="http://www.adv-online.de/namespaces/adv/gid/wfs" xmlns:wfsext=

Alter namespace prefixing with ElementTree in Python

▼魔方 西西 提交于 2019-11-29 11:19:46
问题 By default, when you call ElementTree.parse(someXMLfile) the Python ElementTree library prefixes every parsed node with it's namespace URI in Clark's Notation: {http://example.org/namespace/spec}mynode This makes accessing specific nodes by name a huge pain later in the code. I've read through the docs on ElementTree and namespaces and it looks like the iterparse() function should allow me to alter the way the parser prefixes namespaces, but for the life of me I can't actually make it change

Search and remove element with elementTree in Python

本小妞迷上赌 提交于 2019-11-29 11:12:57
问题 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']

Edit XML file text based on path

谁说胖子不能爱 提交于 2019-11-29 09:46:16
I have an XML file (e.g. jerry.xml) which contains some data as given below. <data> <country name="Peru"> <rank updated="yes">2</rank> <language>english</language> <currency>1.21$/kg</currency> <gdppc month="06">141100</gdppc> <gdpnp month="10">2.304e+0150</gdpnp> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <language>english</language> <currency>4.1$/kg</currency> <gdppc month="05">59900</gdppc> <gdpnp month="08">1.9e-015</gdpnp> <neighbor name="Malaysia" direction="N"/> </country> I