0

Hi i want to parse xml file into json dump. Already tried this:

for i in cNodes[0].getElementsByTagName("person"):
        self.userName = i.getElementsByTagName("login")[0].childNodes[0].toxml()
        self.userPassword = i.getElementsByTagName("password")[0].childNodes[0].toxml()
        self.userNick = i.getElementsByTagName("nick")[0].childNodes[0].toxml()

But i want to get titles and values in format title:value, using for loop.

<user>
<person>
    <nick>Gamer</nick>
    <login>1</login>
    <password>tajne</password>
</person>
<properties>
    <fullHp>100</fullHp>
    <currentHp>25</currentHp>
    <fullMana>200</fullMana>
    <currentMana>124</currentMana>
    <premiumAcc>1</premiumAcc>
</properties>

This is my xml format.

1 Answer 1

1

Don't reinvent the wheel (with "minidom" it would not be fun anyway), use xmltodict:

import xmltodict

data = """
<user>
    <person>
        <nick>Gamer</nick>
        <login>1</login>
        <password>tajne</password>
    </person>
    <properties>
        <fullHp>100</fullHp>
        <currentHp>25</currentHp>
        <fullMana>200</fullMana>
        <currentMana>124</currentMana>
        <premiumAcc>1</premiumAcc>
    </properties>
</user>"""

print xmltodict.parse(data)
Sign up to request clarification or add additional context in comments.

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.