Is there any way to convert XML to python model, instead of write parsing manually?
Asked
Active
Viewed 1,598 times
0
-
Python model??? – juanpa.arrivillaga May 08 '17 at 21:00
-
I try xml.etree.ElementTree and lxml but my file is about 7000 lines alot of element So I try to find automated model generate. – 3zcs May 08 '17 at 21:01
-
I mean python object – 3zcs May 08 '17 at 21:03
-
Possible duplicate of [Converting XML to JSON using Python?](http://stackoverflow.com/questions/191536/converting-xml-to-json-using-python) – tavnab May 08 '17 at 21:09
1 Answers
3
Try xmltodict:
xmltodict
is a Python module that makes working with XML feel like you are working with JSON, as in this "spec":
>>> print(json.dumps(xmltodict.parse("""
... <mydocument has="an attribute">
... <and>
... <many>elements</many>
... <many>more elements</many>
... </and>
... <plus a="complex">
... element as well
... </plus>
... </mydocument>
... """), indent=4))
{
"mydocument": {
"@has": "an attribute",
"and": {
"many": [
"elements",
"more elements"
]
},
"plus": {
"@a": "complex",
"#text": "element as well"
}
}
}

tavnab
- 2,594
- 1
- 19
- 26