2017-05-08 18 views
0

手動で構文解析を書き込むのではなく、XMLをPythonモデルに変換する方法はありますか?XMLをPythonオブジェクトに変換する最も速い方法

+0

をあなたがこれまでに試してみました何? – tavnab

+0

Pythonモデル??? –

+0

私はxml.etree.ElementTreeとlxmlを試していますが、私のファイルは約7000行ですので、私は自動化されたモデルを生成しようとしています。 – 3zcs

答えて

2

xmltodict試してみてください。

xmltodictこの"spec"のように、あなたはJSONで作業しているように感じXMLでの作業になりPythonモジュールです:

>>> 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" 
     } 
    } 
} 
関連する問題