Is there any way to convert XML to python model, instead of write parsing manually?
-
Python model???juanpa.arrivillaga– juanpa.arrivillaga2017-05-08 21:00:30 +00:00Commented May 8, 2017 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– 3zcs2017-05-08 21:01:59 +00:00Commented May 8, 2017 at 21:01
-
I mean python object3zcs– 3zcs2017-05-08 21:03:05 +00:00Commented May 8, 2017 at 21:03
-
Possible duplicate of Converting XML to JSON using Python?tavnab– tavnab2017-05-08 21:09:12 +00:00Commented May 8, 2017 at 21:09
Add a comment
|
1 Answer
Try xmltodict:
xmltodictis 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"
}
}
}