elementtree

ElementTree in Python 2.6.2 Processing Instructions support?

不想你离开。 提交于 2019-12-18 06:25:18
问题 I'm trying to create XML using the ElementTree object structure in python. It all works very well except when it comes to processing instructions. I can create a PI easily using the factory function ProcessingInstruction(), but it doesn't get added into the elementtree. I can add it manually, but I can't figure out how to add it above the root element where PI's are normally placed. Anyone know how to do this? I know of plenty of alternative methods of doing it, but it seems that this must be

How to parse HTML with entities such as   using builtin library ElementTree in Python 2 & Python 3?

流过昼夜 提交于 2019-12-18 05:24:06
问题 There are times that you want to parse some reasonably well-formed HTML pages, but you are reluctant to introduce extra library dependency such as BeautifulSoup or lxml. So you will probably like to try the builtin ElementTree first, because it is a standard library, it is fast (implemented in C), and it supports much better interface (such as XPATH support) than the basic HTMLParser. Not to mention, HTMLParser has its own limitations. ElementTree will work, until it encounters some entities,

Parse XML with (X)HTML entities

╄→尐↘猪︶ㄣ 提交于 2019-12-18 04:38:10
问题 Trying to parse XML, with ElementTree, that contains undefined entity (i.e.   ) raises: ParseError: undefined entity   In Python 2.x XML entity dict can be updated by creating parser (documentation): parser = ET.XMLParser() parser.entity["nbsp"] = unichr(160) but how to do the same with Python 3.x? Update: There was misunderstanding from my side, because I overlooked that I was calling parser.parser.UseForeignDTD(1) before trying to update XML entity dict, which was causing error with the

How can one replace an element with text in lxml?

ε祈祈猫儿з 提交于 2019-12-18 03:59:07
问题 It's easy to completely remove a given element from an XML document with lxml's implementation of the ElementTree API, but I can't see an easy way of consistently replacing an element with some text. For example, given the following input: input = '''<everything> <m>Some text before <r/></m> <m><r/> and some text after.</m> <m><r/></m> <m>Text before <r/> and after</m> <m><b/> Text after a sibling <r/> Text before a sibling<b/></m> </everything> ''' ... you could easily remove every <r>

Python 2.7: type object “ElementTree” has no attribute “register_namespace”

一个人想着一个人 提交于 2019-12-17 22:55:17
问题 with this python 2.7.3 (or 2.7.0) code I want to change the value of the attribute "android:versionCode='2'", which has the namespace prefix "android": #!/usr/bin/python from xml.etree.ElementTree import ElementTree, dump import sys, os # Problem here: ElementTree.register_namespace("android", "http://schemas.android.com/apk/res/android") tree = ElementTree() tree.parse("AndroidManifest.xml") root = tree.getroot() root.attrib["{http://schemas.android.com/apk/res/android}versionCode"] = "3"

ElementTree findall() returning empty list

▼魔方 西西 提交于 2019-12-17 16:18:40
问题 I am trying to write a small script for interacting with the last.fm API. I have a small bit of experience working with ElementTree , but the way I used it previously doesn't seem to be working, it instead returns an empty list. I removed the API key as I don't know exactly how private it should be, and gave an example of the XML I am receiving in it's place. Class for interacting with API: from xml.etree import ElementTree import urllib import urllib2 class Last_fmWrapper(object): def __init

Saving XML files using ElementTree

血红的双手。 提交于 2019-12-17 07:25:37
问题 I'm trying to develop simple Python (3.2) code to read XML files, do some corrections and store them back. However, during the storage step ElementTree adds this namespace nomenclature. For example: <ns0:trk> <ns0:name>ACTIVE LOG</ns0:name> <ns0:trkseg> <ns0:trkpt lat="38.5" lon="-120.2"> <ns0:ele>6.385864</ns0:ele> <ns0:time>2011-12-10T17:46:30Z</ns0:time> </ns0:trkpt> <ns0:trkpt lat="40.7" lon="-120.95"> <ns0:ele>5.905273</ns0:ele> <ns0:time>2011-12-10T17:46:51Z</ns0:time> </ns0:trkpt> <ns0

Can ElementTree be told to preserve the order of attributes?

限于喜欢 提交于 2019-12-17 05:02:15
问题 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

Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method “find”, “findall”

﹥>﹥吖頭↗ 提交于 2019-12-16 22:10:31
问题 I want to use the method of "findall" to locate some elements of the source xml file in the ElementTree module. However, the source xml file (test.xml) has namespace. I truncate part of xml file as sample: <?xml version="1.0" encoding="iso-8859-1"?> <XML_HEADER xmlns="http://www.test.com"> <TYPE>Updates</TYPE> <DATE>9/26/2012 10:30:34 AM</DATE> <COPYRIGHT_NOTICE>All Rights Reserved.</COPYRIGHT_NOTICE> <LICENSE>newlicense.htm</LICENSE> <DEAL_LEVEL> <PAID_OFF>N</PAID_OFF> </DEAL_LEVEL> </XML

Parsing soap/XML response in Python

回眸只為那壹抹淺笑 提交于 2019-12-14 03:58:19
问题 I am trying to parse the below xml using the python. I do not understand which type of xml this is as I never worked on this kind of xml.I just got it from a api response form Microsoft. Now my question is how to parse and get the value of BinarySecurityToken in my python code. I refer this question Parse XML SOAP response with Python But look like this has also some xmlns to get the text .However in my xml I can't see any nearby xmlns value through I can get the value. Please let me know how