Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 parse xml

I tried to parse XML using different python3 modules and different articles from internet but not success.

I have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Header/>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<cwmp:GetParameterValuesResponse>
    <ParameterList SOAP-ENC:arrayType="cwmp:ParameterValueStruct[3]">
        <ParameterValueStruct>
            <Name>SOME_NAME_1_HERE</Name>
            <Value>2</Value>
        </ParameterValueStruct>
        <ParameterValueStruct>
            <Name>SOME_NAME_2_HERE</Name>
            <Value>180</Value>
        </ParameterValueStruct>
        <ParameterValueStruct>
            <Name>SOME_NAME_3_HERE</Name>
            <Value>1800</Value>
        </ParameterValueStruct>
    </ParameterList>
</cwmp:GetParameterValuesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I need to take data from XML tags: Name and Value It should be something like:

SOME_NAME_1_HERE 2
SOME_NAME_2_HERE 180
SOME_NAME_3_HERE 1800

How I can get this values using Python3(will be good to use python default modules - not bs4)?

Thanks

like image 879
Petro Pikh Avatar asked Apr 24 '17 13:04

Petro Pikh


People also ask

How do I parse XML in Python 3?

Parsing XML with DOM APIs If you are looking at one SAX element, you have no access to another. Here is the easiest way to load an XML document quickly and to create a minidom object using the xml. dom module. The minidom object provides a simple parser method that quickly creates a DOM tree from the XML file.

How do you parse an XML string in Python?

There are two ways to parse the file using 'ElementTree' module. The first is by using the parse() function and the second is fromstring() function. The parse () function parses XML document which is supplied as a file whereas, fromstring parses XML when supplied as a string i.e within triple quotes.

Which XML parser is best in Python?

If parsing speed is a key factor for you, consider using cElementTree or lxml.

Which module can you use to parse an XML file using Python?

Python Module used: This article will focus on using inbuilt xml module in python for parsing XML and the main focus will be on the ElementTree XML API of this module.

How to parse an XML file in Python?

Basically, python allows us to parse the XML document by using two different modules that we called as xml.etree and Element Tree module. Normally parsing means it reads the data from the different file and splits it into the different pieces that are the XML file.

Should you use Zen of Python for XML parsing?

On the one hand, the Zen of Python promises only one obvious way to achieve your goal. At the same time, the standard library follows the batteries included motto by letting you choose from not one but several XML parsers. Luckily, the Python community solved this surplus problem by creating even more XML parsing libraries.

What is parsing in XML?

Normally parsing means it reads the data from the different file and splits it into the different pieces that are the XML file. Therefore, we need to use different elements such as Tag, Text string, Attributes, Tail String, etc.

What is XML processing in Python 3?

Python 3 - XML Processing. XML is a portable, open source language that allows programmers to develop applications that can be read by other applications, regardless of operating system and/or developmental language.


2 Answers

Using xml.etree you can execute simple XPath expression .//element_name to find element anywhere within a given context element :

from xml.etree import ElementTree as ET
tree = ET.parse('path_to_your_xml.xml')
root = tree.getroot()

for p in root.findall('.//ParameterValueStruct'):
    print("%s | %s" % (p.find('Name').text, p.find('Value').text))
like image 163
har07 Avatar answered Oct 07 '22 00:10

har07


You can try something like this :

import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('Newfile.xml').getroot()

print(e)
for atype in e.findall('.//ParameterValueStruct'):
    print("%s | %s" % (atype.find('Name').text, atype.find('Value').text))
like image 43
Reda Maachi Avatar answered Oct 07 '22 00:10

Reda Maachi