elementtree

How to tell lxml.etree.tostring(element) not to write namespaces in python?

谁都会走 提交于 2019-11-28 00:23:17
问题 I have a huge xml file (1 Gig). I want to move some of the elements (entrys) to another file with the same header and specifications. Let's say the original file contains this entry with tag <to_move> : <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE some SYSTEM "some.dtd"> <some> ... <to_move date="somedate"> <child>some text</child> ... ... </to_move> ... </some> I use lxml.etree.iterparse to iterate through the file. Works fine. When I find the element with tag <to_move> , let's

Cannot write XML file with default namespace [duplicate]

本秂侑毒 提交于 2019-11-27 23:54:02
问题 This question already has an answer here: Saving XML files using ElementTree 5 answers I'm writing a Python script to update Visual Studio project files. They look like this: <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> ... The following code reads and then writes the file: import xml.etree.ElementTree as ET tree = ET.parse(projectFile) root = tree.getroot() tree.write

Merge xml files with nested elements without external libraries

僤鯓⒐⒋嵵緔 提交于 2019-11-27 19:31:27
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>new text</nested2> </elements> </root> What I have tried: From this answer . from xml.etree import

Converting xml to dictionary using ElementTree

你离开我真会死。 提交于 2019-11-27 18:01:03
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. 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 third line in the function body to use a different key. Also, you can't handle mixed content with this.

Python 2.5.4 - ImportError: No module named etree.ElementTree

六眼飞鱼酱① 提交于 2019-11-27 16:28:00
问题 I'm running Python 2.5.4 on Windows and I keep getting an error when trying to import the ElementTree or cElementTree modules. The code is very simple (I'm following a tutorial): import xml.etree.ElementTree as xml root = xml.Element('root') child = xml.Element('child') root.append(child) child.attrib['name'] = "Charlie" file = open("test.xml", 'w') xml.ElementTree(root).write(file) file.close() I get the error message when I run it from the cmd or but not when I directly try it from the

Using SimpleXMLTreeBuilder in elementtree

佐手、 提交于 2019-11-27 16:10:27
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? If you have third party module that wants to use ElementTree (and XMLTreeBuilder by dependency) you can change ElementTree's XMLTreeBuilder definition to the one

Python: ElementTree, get the namespace string of an Element

旧街凉风 提交于 2019-11-27 14:35:22
问题 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

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

孤街浪徒 提交于 2019-11-27 14:31:27
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 then reads/parses this XML using ElementTree, and then for every app setting matching a certain key ("foo

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

孤者浪人 提交于 2019-11-27 14:30:58
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) File "lxml.etree.pyx", line 2992, in lxml.etree.fromstring (src\lxml\lxml.etree.c:62311) File "parser.pxi

Python and ElementTree: return “inner XML” excluding parent element

我是研究僧i 提交于 2019-11-27 13:54:52
问题 In Python 2.6 using ElementTree, what's a good way to fetch the XML (as a string) inside a particular element, like what you can do in HTML and javascript with innerHTML? Here's a simplified sample of the XML node I am starting with: <label attr="foo" attr2="bar">This is some text <a href="foo.htm">and a link</a> in embedded HTML</label> I'd like to end up with this string: This is some text <a href="foo.htm">and a link</a> in embedded HTML I've tried iterating over the parent node and