1

I'm new to Python and I need to fetch some of the attribute values from a XML file and then to convert these values to JSON.

example:

<messages> 
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Dont forget me this weekend!</body>
</messages>

{
"from":"Jani",
"body":"Dont forget me this weekend!"
}

How can I best achieve this? Would I convert it to JSON first? If yes, which library would I use? Lxml? Or would I convert it to String and then use regex to get some of the values?

2 Answers 2

2

You can use the ElementTree API to parse your XML text and get the desired values out of it.

Then you can use the JSON API to create the desired JSON output.

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

Comments

1

I notice that your json format is just like a python dict.

from xml.dom import minidom

data = {}
with open('email.xml', 'r', encoding='utf8') as fh:
    dom = minidom.parse(fh)
# Get root element
root = dom.documentElement
for tag in root.childNodes:
    if tag.nodeType != tag.TEXT_NODE:
        key = tag.nodeName
        value = tag.childNodes[0].data
        data[key] = value
print(data)

Output is: {'to': 'Tove', 'from': 'Jani', 'heading': 'Reminder', 'body': 'Dont forget me this weekend!'}

If u want to understand xml more deeply, some component you need to know, such as root, element, node, tag, attribute and so on.

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.