简体   繁体   中英

Getting values of XML attributes In Python

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM