Reputation: 437
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?
Upvotes: 1
Views: 139
Reputation: 25
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.
Upvotes: 1
Reputation: 2991
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.
Upvotes: 2