私はpythonとxmlを解析するのは初めてですが、私が扱うプログラムで吐き出される特定のXMLファイルに問題があります。私は、このXMLファイルをpythonとelementtreeを使って解析して、URLデータを抽出しています(下のURLは偽です)。なぜこれが機能していないのかについてのアイデアはありますか?python、elementtree、xml parserを使用して何らかの理由で属性が機能しないのですか?
私のpythonコード:
<Query id="f9cef041-085d-47e0-8d16-15e36bba1ec8" name="">
<Description />
<JustSortedColumns />
<Conditions linking="All">
<Condition class="PDCT" enabled="True" readOnly="False" linking="Any">
<Condition class="SMPL" enabled="True" readOnly="False">
<Operator id="Contains" />
<Expressions>
<Expr class="ENTATTR" id="Person.LinkedInUrl" />
<Expr class="CONST" type="String" kind="Scalar" value="https://www.linkedin.com/Bill-Smith" text="https://www.linkedin.com/Bill-Smith" />
</Expressions>
</Condition>
</Condition>
</Conditions>
</Query>
私は自分自身を書いた他、テスト、xmlファイルにうまく作品を書いたのpython:
def xmlTreeParser(fileName,attribute,tagName):
tree = ET.parse(fileName)
root = tree.getroot()
attribArray = [element.attrib[attribute] for element in root.findall(tagName)]
print attribArray
xmlTreeParser("xml_file.xml",'text','Expr')
ここに私のxmlファイルです。私はXMLのこの特定のブロックを解析できない理由について紛失しています。みんな、ありがとう。
'xpath'を調べると' findall'は再帰的ではありません。ルートの子供は3人しかいませんが、誰も 'text'という属性を持っていないので、何も得られません。 'xpath'は木の中のもっと遠くにあるものを探すことができます。 –