FAQ Database Discussion Community


Parsing XML API Result Using ElementTree

python,xml,python-2.7,elementtree
I am using Element Tree to parse the following result of an API call. <response status="success" code="19"> <result total-count="1" count="1"> <users> <entry name="admin"> <phash>$$$$$</phash> <permissions> <role-based> <superuser>yes</superuser> </role-based> </permissions> </entry> <entry name="tester"> <permissions> <role-based> <superuser>yes</superuser> </role-based> </permissions>...

elementtree - get title?

python,xml,elementtree
<item rdf:about="http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-0005"> <title>CVE-2014-0005</title> <link>http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-0005</link> <description>PicketBox and JBossSX, as used in Red Hat JBoss Enterprise Application Platform (JBEAP) 6.2.2 and JBoss BRMS before 6.0.3 roll up patch 2, allows remote authenticated users to read and modify the application sever configuration and state by deploying a crafted application.</description>...

Reading XML data from a file in python using Element tree

python,xml,elementtree
I am trying to print data from my xml file but using ElementTree is not helping. Data is being successfully written into the file 'data.xml' but cant be read, console terminated with exit code 0 without displaying any data. The 'data.findall' loop is not even being entered into. import urllib2...

How do I escape colons in an attribute name with Python's ElementTree?

python,xml,escaping,python-2.6,elementtree
Background I am using ElementTree in Python version 2.6 to create an XML file (using data retrieved from a database). Code The following line of code is the problem area, as I keep getting a syntax error because of the colons within my attribute names. # Please ignore any errors...

Compare python xml item output to list

python,xml,elementtree,xml.etree
I am farely new at python. I am currently working on a little project and I have a XML document in this structure. <commands> <level1 name="sh">show></level1> <level2 name="ip">ip</level2> <level3 name="int">interface</level3> <level4 name="br">show ip interface brief</level4> <level1 name="int>interface</level1> </commands> What I need to do is to extract the attribute values from...

ElementTree find returns None?

python,xml,elementtree
I'm using ElementTree with Python to parse an XML file to find the contents of the tag contentType. Here's the Python line: extensionType = ET.parse("src/" + str(filename)).find('contentType') And here's the XML: <?xml version="1.0" encoding="UTF-8"?> <StaticResource xmlns="http://soap.sforce.com/2006/04/metadata"> <cacheControl>Private</cacheControl> <contentType>image/jpeg</contentType> </StaticResource> What am I doing wrong? Thanks!...

Extract part of xml file with python etree

python,xml,elementtree
I have big xml file that looks as one below. I have put just part of it, as it is >2gb, so just that you see the structure. Basicly all SubNetwork parents have same structure as the one I showed below. What I want to do is to extract only...

Why does cElementTree iterparse return None elements?

python,xml,elementtree
I am trying to parse an xml file with cElementTree.iterparse. However, I can't understand what is going on because iterparse returns empty elements. I have an xml file that has the following approximate layout: <DOCS> <ID id="1"> <HEAD>title1</HEAD> <DATE>21.01.2010</DATE> <TEXT> <P>some text</P> <P>some text</P> <P>some text</P> </TEXT> </ID> <ID id="2">...

Using XML python library to acces data

python,xml,algorithm,xml-parsing,elementtree
I have a XML string as the following: <?xml version="1.0" encoding="UTF-8" ?>\n<data serial="1234">\n <ts>1418823725</ts>\n <r t="P" n="AA"><v>1596787</v><i>62</i></r>\n <r t="P" n="BB"><v>246995</v><i>63</i></r>\n <r t="P" n="CC"><v>0</v><i>0</i></r>\n</data>\n I am trying to parse the string to get the this using xml library in python as follows: for child in root.iter('r'): print child.attrib.get('t')...

memory efficient way to change and parse a large XML file in python

python,xml,parsing,sax,elementtree
I want to parse a large XML file (25 GB) in python, and change some of its elements. I tried ElementTree from xml.etree but it takes too much time at the first step (ElementTree.parse). I read somewhere that SAX is fast and do not load the entire file into the...

Python ElementTree XML Parsing with Multiple Answers

python,xml-parsing,elementtree
I am parsing movie.xml file generated by My Movies 5 to get genres for a movie. Some movies have multiple genres, like: -<Genres> <Genre>Adventure</Genre> <Genre>Comedy</Genre> <Genre>Action</Genre> ... ... </Genres> How can I read it as a single variable genres = genere1, genre2, genre3 ... This is what I am doing,...

Removing elements with etree

python,elementtree
Under Relationship I want to keep only elements that have TO_FDN="FtpServer=,and remove all others. How can I do it with etree in python 2.6? <Relationship> <AssociableNode AssociationType="ManagedElement_to_ftpBackupStore" TO_FDN="FtpServer=BACKUP,FtpService=BACKUP" /> <AssociableNode AssociationType="ManagedElement_to_ftpLicenseKeyStore" TO_FDN="FtpServer=LICENSE,FtpService=LICENSE" /> <AssociableNode AssociationType="ManagedElement_to_ftpSwStore" TO_FDN="FtpServer=SOFTWARE,FtpService=SOFTWARE_RBS" />...

Create XML file by iterating over lists in python

python,xml-parsing,elementtree
I have checked this link but it doesnt solved my problem. I have 2 lists: a = [['txt','stxt','pi','min','max'],['txt1','stxt1','pi1','min1','max1']] b = [[0.45,1.23],[[0.75,1.53]] for l1 in a: for l2 in b: root = ET.Element("Class ",name = l1[0]) doc = ET.SubElement(root, "subclass" , name = l1[1]) ET.SubElement(doc, l1[4], min = str(l2 [0]),max =...

How can I use ElementTree to find all top level matched nodes?

python,xml,elementtree
I have an unknown XML structure where I would like to find all nodes with a Name attribute containing FooBar that don't themselves have a similar ancestor. That is to say, all highest level matching nodes excluding their children. This is the current prototype code I have for finding nodes...

Concatenate two XML templates in Python

python,xml,elementtree
I have two XML template needed to be added when certain conditions are met. I concatenate doesn't seem to handle them properly. template1 = \ ''' <ParentTag> <orders orderid="%s"> <unitprice>%s</unitprice> <quantity>%s</quantity> </orders> </ParentTag> ''' template2 = \ ''' <details> <productid>%s</productid> <productname>%s</productname> </details> My objective is to get an xml file...

Extract Text from HTML Python (BeautifulSoup, RE, Other Option?)

python,web-scraping,beautifulsoup,elementtree
I am familiar with BeautifulSoup and Regular Expressions as a means of extracting text from HTML but not as familiar with others, such as ElementTree, Minidom, etc. My question is fairly straightforward. Given the HTML snippet below, which library is best for extracting the text below? The text being the...

How to get the child of child using Python's ElementTree

python,elementtree
I'm building a Python file that communicates with a PLC. When compiling, the PLC creates a XML file that delivers important information about the program. The XML looks more less like this: <visu> <time>12:34</time> <name>my_visu</name> <language>english</language> <vars> <var name="input1">2</var> <var name="input2">45.6</var> <var name="input3">"hello"</var> </vars> </visu> The important part is found...

Python error: EOL while scanning string literal

xml,sqlite,python-2.6,elementtree,eol
Some background information I am using SQLite to access a database and retrieve the desired information. I'm using ElementTree in Python version 2.6 to create an XML file with that information. Code Here is the code I'm using to create an XML file from the database schema. I've denoted the...

Append parent to xml

python,lxml,elementtree
I want to add one more block to xml file. Basicly under parent Tss I want to create sublement Entry with its attributes. Here is what I want to add to xml file: <Entry> <System string = "rbs005019"/> <Type string = "SECURE"/> <User string = "rbs"/> <Password string = "rbs005019"/>...

Python and ElementTree: How can I nest elements/subelements in an XML?

python,xml,dictionary,python-2.6,elementtree
Background I am using Python 2.6, ElementTree, and SQLite3. My script currently does the following: Connects to a database to retrieve information from tables/schema Adds necessary data to an XML tree Outputs a (currently incorrect) XML file Code Here is how I've been retrieving schema data and adding elements to...

Recursive XML parsing python using ElementTree

python,xml,recursion,elementtree
I'm trying to parse below XML using Python ElementTree to product output as below. I'm trying to write modules for top elements to print them. However It is slightly tricky as category element may or may not have property and cataegory element may have a category element inside. I've referred...

Conditional Search in XML Python

python,xml,xml-parsing,elementtree
I have a xml file Orders.xml (excerpt follows): <?xml version="1.0"?> <ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01"> <ListOrdersResult> <Orders> <Order> <LatestShipDate>2015-06-02T18:29:59Z</LatestShipDate> <OrderType>StandardOrder</OrderType> <PurchaseDate>2015-05-31T03:58:30Z</PurchaseDate> <AmazonOrderId>171-6355256-9594715</AmazonOrderId> <LastUpdateDate>2015-06-01T04:18:58Z</LastUpdateDate>...

Python removing elements from large xml file with xml iterparse

python,xml,elementtree
I'm a completely newbie to Python and have been using it recently to try and parse a large-ish xml file 700mb. Having looked around I have been attempting to use the iterparse methods to to remove a element called Revision_History for the XML since we no longer require this information....

print tag value from xml using python

python,xml,elementtree
I am trying to write a Python script that prints the value of a particular tag in an XML output. Here, the tag value I need to print is the value of in each and every occurrences in the XML output. I tried as below, but it shows an attribute...

Parsing XML in Python with ElementTree - findall

python,xml,elementtree
I'm using the documentation here to try to get only the values for certain elements. This is an example of the structure of my XML: <ListOrdersRequest> <ListOrdersResult> <Orders> <Order> ... <ElementIWant>value_I_want</ElementIWant> </Order> <Order> ... <ElementIWant>value_I_want</ElementIWant> </Order> ... </Orders> </ListOrdersResult> </ListOrdersRequest> I have this entire XML file saved to string variable...

Parsing an XML file using ElementTree

python,xml,parsing,elementtree
I am having two issues with parsing an XML file. I want to only return one set of properties I.E only the property values under the first Process and I want to return the second Source under the second Process. When I use my code it returns the Source under...

Removing child elements in XML using python

python,xml,elementtree
Python 3.2.5 x64 ElementTree I have data that I need to format using python. Essentially I have file with elements and subelements. I need to delete the child elements of some of these elements. I have checked previous questions and I couldn't make a solution. The best I had so...

Python Elementtree delete/edit a node

python,xml,elementtree
I am currently creating a project in python that requires xml manipulation. To manipulate the xml file, I will use Elementtree. Never worked with that module before. I used to use php, but is complety different. I have the following xml file: <myvideos> <video> <title>video1</title> <plot>description bla bla bla</plot> <duration>50</duration>...

How do I get Python's ElementTree to pretty print to an XML file?

python,xml,python-2.6,elementtree,pretty-print
Background I am using SQLite to access a database and retrieve the desired information. I'm using ElementTree in Python version 2.6 to create an XML file with that information. Code import sqlite3 import xml.etree.ElementTree as ET # NOTE: Omitted code where I acccess the database, # pull data, and add...

parsing XML from a string into an element

python,xml,unicode,elementtree
I have had some troubles to parse a XML from a string directly into an Element. I a have an xml file that I have transform into a string: resp = requests.post(request_url, request_string, proxies=urllib.getproxies(), stream=True) And as recommended here: http://stackoverflow.com/a/25023776/1551810, I used the contenet instead of the text: response_tree =...

unable to parse graphml file with elementtree

python,xml,elementtree
xml <?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G" edgedefault="undirected"> <node id="n0"/> <node id="n1"/> <edge id="e1" source="n0" target="n1"/> </graph> </graphml> python code tree = ET.parse(my_file.xml).getroot() print tree.findall('graph') # returns [] If I remove the attributes from the...

Python Logic Issue

python,sqlite,elementtree
I am using elementtree to parse an XML file and placing the data into an sqlite database. I have come across a problem that I believe could be solved by some better logic, that I am most likely missing. I am getting a local variable 'netbios_name' referenced before assignment error,...

xml.etree.ElementTree.Element.remove not removing all elements

python,xml,elementtree
Please see the following code: import xml.etree.ElementTree as ET for x in ("<a><b /><c><d /></c></a>", "<a><q /><b /><c><d /></c></a>", "<a><m /><q /><b /><c><d /></c></a>"): root = ET.fromstring(x) for e in root: root.remove(e) print(ET.tostring(root)) I expect it to output <a></a> in all instances but instead it gives: b'<a><c><d /></c></a>' b'<a><b /></a>'...

How to read out tc result verdict in an xml file with Python and ElementTree

python-2.7,elementtree
I'm using Python 2.7.8 and have a problem using ElementTree to read out specific elements. I have an xml with several <ATC> </ATC> tags. For every of this tag I want to read out tcName and the verdict result for that tcName. I'm trying to read out TestCase name ("REQPROD...