-1

Is there any way to convert XML to python model, instead of write parsing manually?

4
  • Python model??? Commented 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. Commented May 8, 2017 at 21:01
  • I mean python object Commented May 8, 2017 at 21:03
  • Possible duplicate of Converting XML to JSON using Python? Commented May 8, 2017 at 21:09

1 Answer 1

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"
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.