1

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?

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.