elementtree

How to prevent xml.ElementTree fromstring from dropping commentnode

不羁的心 提交于 2019-11-26 20:58:10
问题 I have tho following code fragment: from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for elem in mathml.getiterator(): elem.tag = 'm:' + elem.tag return tostring(mathml) When i input the following input : <math> <a> 1 2 3 </a> <b /> <foo>Uitleg</foo> <!-- <bar> --> </math> It results in: <m:math> <m:a> 1 2 3 </m:a> <m:b /> <m:foo>Uitleg</m:foo> </m:math> How come? And how can I preserve the comment? edit : I don't care for the exact xml library used, however, I

Converting xml to dictionary using ElementTree

放肆的年华 提交于 2019-11-26 19:14:57
问题 I'm looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes. 回答1: def etree_to_dict(t): d = {t.tag : map(etree_to_dict, t.iterchildren())} d.update(('@' + k, v) for k, v in t.attrib.iteritems()) d['text'] = t.text return d Call as tree = etree.parse("some_file.xml") etree_to_dict(tree.getroot()) This works as long as you don't actually have an attribute text ; if you do, then change the

How do I parse and write XML using Python's ElementTree without moving namespaces around?

北战南征 提交于 2019-11-26 18:24:47
问题 Our project gets from upstream XML of this form: <?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="7.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> <appSettings> <add key="foo" value="default"> ... </appSettings> </configuration> It

Testing Equivalence of xml.etree.ElementTree

ⅰ亾dé卋堺 提交于 2019-11-26 18:15:33
问题 I'm interested in equivalence of two xml elements; and I've found that testing the tostring of the elements works; however, that seems hacky. Is there a better way to test equivalence of two etree Elements? Comparing Elements directly: import xml.etree.ElementTree as etree h1 = etree.Element('hat',{'color':'red'}) h2 = etree.Element('hat',{'color':'red'}) h1 == h2 # False Comparing Elements as strings: etree.tostring(h1) == etree.tostring(h2) # True 回答1: This compare function works for me:

Can ElementTree be told to preserve the order of attributes?

会有一股神秘感。 提交于 2019-11-26 17:45:00
I've written a fairly simple filter in python using ElementTree to munge the contexts of some xml files. And it works, more or less. But it reorders the attributes of various tags, and I'd like it to not do that. Does anyone know a switch I can throw to make it keep them in specified order? Context for this I'm working with and on a particle physics tool that has a complex, but oddly limited configuration system based on xml files. Among the many things setup that way are the paths to various static data files. These paths are hardcoded into the existing xml and there are no facilities for

Using SimpleXMLTreeBuilder in elementtree

痴心易碎 提交于 2019-11-26 17:25:36
问题 I have been developing an application with django and elementtree and while deploying it to the production server i have found out it is running python 2.4. I have been able to bundle elementtree but now i am getting the error: "No module named expat; use SimpleXMLTreeBuilder instead" Unfortunately i cannot upgrade python so im stuck with what i got. How do i use SimpleXMLTreeBuilder as the parser and/or will i need to rewrite code? 回答1: If you have third party module that wants to use

Find element by text with XPath in ElementTree

房东的猫 提交于 2019-11-26 17:23:34
问题 Given an XML like the following: <root> <element>A</element> <element>B</element> </root> How can I match the element with content A using ElementTree and its support for XPath? Thanks 回答1: AFAIK ElementTree does not support XPath. Has it changed? Anyway, you can use lxml and the following XPath expression: import lxml.etree doc = lxml.etree.parse('t.xml') print doc.xpath('//element[text()="A"]')[0].text print doc.xpath('//element[text()="A"]')[0].tag The result will be: A element 回答2: If you

parsing XML file gets UnicodeEncodeError (ElementTree) / ValueError (lxml)

六月ゝ 毕业季﹏ 提交于 2019-11-26 16:56:12
问题 I send a GET request to the CareerBuilder API : import requests url = "http://api.careerbuilder.com/v1/jobsearch" payload = {'DeveloperKey': 'MY_DEVLOPER_KEY', 'JobTitle': 'Biologist'} r = requests.get(url, params=payload) xml = r.text And get back an XML that looks like this. However, I have trouble parsing it. Using either lxml >>> from lxml import etree >>> print etree.fromstring(xml) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> print etree.fromstring(xml)

Merge xml files with nested elements without external libraries

心已入冬 提交于 2019-11-26 15:58:25
问题 I am trying to merge multiple XML files together using Python and no external libraries. The XML files have nested elements. Sample File 1: <root> <element1>textA</element1> <elements> <nested1>text now</nested1> </elements> </root> Sample File 2: <root> <element2>textB</element2> <elements> <nested1>text after</nested1> <nested2>new text</nested2> </elements> </root> What I Want: <root> <element1>textA</element1> <element2>textB</element2> <elements> <nested1>text after</nested1> <nested2

Parsing text from XML node in Python

帅比萌擦擦* 提交于 2019-11-26 14:57:41
问题 I'm trying to extract URLs from a sitemap like this: https://www.bestbuy.com/sitemap_c_0.xml.gz I've unzipped and saved the .xml.gz file as an .xml file. The structure looks like this: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas