elementtree

root of the xml file is giving as NONE why?

白昼怎懂夜的黑 提交于 2019-12-02 07:46:36
from elementtree import ElementTree as ET tree= ET.parse(r'N:\myinternwork\files xml of bus systems\testonieeebus.xml','r') root= tree.getroot() print(root) now the error is in output as it is giving none <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <author>Giada De Laurentiis</author> </book> </bookstore> The following code is enough. You don't need to open the file at beginning. ET.parse will do it for you if you provide the correct path. In your code you import the library as ET and then reassign the same variable by opening the file. So ET.parse() referes to

Python 2.5: ElementTree and UML in XML

跟風遠走 提交于 2019-12-02 07:40:35
问题 I'm working with an XML file which represent an UML model. Here is an example of what it is: <?xml version="1.0" encoding="utf-8"?> <XMI xmi.version="1.1" xmlns:UML="omg.org/UML13"> <XMI.content> <UML:Model name="Model" xmi.id="_0"> <UML:Namespace.ownedElement> <UML:Package name="Standard" xmi.id="_5"> </UML:Package> </UML:Namespace.ownedElement> </UML:Model> </XMI.content> </XMI> It is a Rhapsody import format. I want to modify this XML file by using ElementTree in Python 2.5. I have at

Merging Lots of XML files

北城以北 提交于 2019-12-02 07:27:41
I have lots of xml files that I need to merge. I have tried this link at merging xml files using python's ElementTree whose code is (Edited as per my need): import os, os.path, sys import glob from xml.etree import ElementTree def run(files): xml_files = glob.glob(files +"/*.xml") xml_element_tree = None for xml_file in xml_files: print xml_file data = ElementTree.parse(xml_file).getroot() # print ElementTree.tostring(data) for result in data.iter('TALLYMESSAGE'): if xml_element_tree is None: xml_element_tree = data insertion_point = xml_element_tree.findall("./BODY/DATA/TALLYMESSAGE")[0] else

Preserving XML attribute order?

本秂侑毒 提交于 2019-12-02 07:21:03
问题 I know this question has been asked in the past, but they have all been dated a few years back. I am wondering if there has been any changes made to Python modules such as lxml, minidom, or etree that will allow us to preserve the attribute order in XML files without patching. I need the order to be preserved as the program I am supplying the files to relies on it. If there are no updates, what's the easiest way to implement this? 回答1: The insignificance of attribute ordering is not a

Python 2.5: ElementTree and UML in XML

 ̄綄美尐妖づ 提交于 2019-12-02 05:14:12
I'm working with an XML file which represent an UML model. Here is an example of what it is: <?xml version="1.0" encoding="utf-8"?> <XMI xmi.version="1.1" xmlns:UML="omg.org/UML13"> <XMI.content> <UML:Model name="Model" xmi.id="_0"> <UML:Namespace.ownedElement> <UML:Package name="Standard" xmi.id="_5"> </UML:Package> </UML:Namespace.ownedElement> </UML:Model> </XMI.content> </XMI> It is a Rhapsody import format. I want to modify this XML file by using ElementTree in Python 2.5. I have at least one problem but I found 2 consequences, here they are: With this simple code: import xml.etree

Print all xml child node using python

一笑奈何 提交于 2019-12-02 03:49:35
I want to print all the values of the "ClCompiler" child of "ItemGroup" of my xml file. my python code tree = minidom.parse(project_path) itemgroup = tree.getElementsByTagName('ItemGroup') print (itemgroup[0].toxml()) my result <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|Win32"> <Configuration>Release</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> </ItemGroup> <ItemGroup> <ClCompile Include="../../avmedia

SyntaxError using gdata-python-client to access Google Book Search Data API

匆匆过客 提交于 2019-12-02 02:09:42
>>> import gdata.books.service >>> service = gdata.books.service.BookService() >>> results = service.search_by_keyword(isbn='0434003484') Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> results = service.search_by_keyword(isbn='0434003484') ... snip ... File "C:\Python26\lib\site-packages\atom\__init__.py", line 127, in CreateClassFromXMLString tree = ElementTree.fromstring(xml_string) File "<string>", line 85, in XML SyntaxError: syntax error: line 1, column 0 This is a minimal example -- in particular, the book service unit tests included in the package also fail

lxml.etree insert elements into element.text

▼魔方 西西 提交于 2019-12-02 02:04:08
I have strings that have empty xml elements in them, like this: >>> s = """fizz buzz <pb n="44"/> bananas""" These strings have been assigned to xml elements using the etree.SubElement method: >>> from lxml import etree as et >>> root = et.Element('root') >>> txt = et.SubElement(root, 'text') >>> txt.text = s >>> et.dump(root) <root> <text>fizz buzz <pb n="44"/> bananas</text> </root> Fiddling about with re.split() and etree's text and tail I can insert a subelement <pb n="44"/> where I want it in txt.text ; however, sometimes I've got multiple occurrences of the <pb/> element in the string,

Using XPATH in python etree to select node with out a specific attribute

落爺英雄遲暮 提交于 2019-12-01 23:09:43
问题 Following is my xml file contents, <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E" distance="500"/> <neighbor name="Switzerland" direction="W"/> </country> </data> Following is my code, tree = ET.parse(fileName) doc = tree.getroot() #nodes = doc.findall(".//country/neighbor") #works #nodes = doc.findall(".//country/neighbor[@direction]") #works nodes = doc.findall(".//country/neighbor[not

Using XPATH in python etree to select node with out a specific attribute

﹥>﹥吖頭↗ 提交于 2019-12-01 21:47:33
Following is my xml file contents, <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E" distance="500"/> <neighbor name="Switzerland" direction="W"/> </country> </data> Following is my code, tree = ET.parse(fileName) doc = tree.getroot() #nodes = doc.findall(".//country/neighbor") #works #nodes = doc.findall(".//country/neighbor[@direction]") #works nodes = doc.findall(".//country/neighbor[not(@direction)]") #not working I am getting the following error, File "C:\Python27\lib\xml\etree