Reputation: 105
I have an XML that I would like to parse then send the result into a txt file.
The XML file is build from blocks like this one :
<rpc-reply message-id="12"
xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<user-stats>
<allocated-user-count>10</allocated-user-count>
<current-user-count>3</current-user-count>
<max-active-user-count-24hrs>2</max-active-usercount-
24hrs>
<min-active-user-count-24hrs>0</min-active-usercount-
24hrs>
</user-stats>
</rpc-reply>
All the blocks will start with and and with
I would like to remove all the tags and get the results by line and copy them in a text file. For example :
allocated user count <tab> current-user-count <tab> max-user-count
100 <tab> 1<tab> 2<tab>
100 <tab> 0<tab> 2<tab> (info taken from the second RPC block)
etc
Is there any way I can do this?
Upvotes: 1
Views: 505
Reputation: 406
Yes, there are ways you can do this.
The most accessible would be to use the XML Processing Modules, included in Python's standard library, to read the XML, and then use a text file object to output to the text file after doing your processing.
Upvotes: 0
Reputation: 77033
You can use the xmltodict module for this.
You will need to install it using
pip install xmltodict
Once you've it, just do
import xmltodict
data = xmltodict.parse(xmldata)
This will give you an early traversable python dict, which you can then use to get the values you want.
Upvotes: 1