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
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.
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.
If parsing speed is a key factor for you, consider using cElementTree or lxml.
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.
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.
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.
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.
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.
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))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With