私のXML文字列は -
xmlData = """<SMSResponse xmlns="http://example.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Cancelled>false</Cancelled>
<MessageID>00000000-0000-0000-0000-000000000000</MessageID>
<Queued>false</Queued>
<SMSError>NoError</SMSError>
<SMSIncomingMessages i:nil="true"/>
<Sent>false</Sent>
<SentDateTime>0001-01-01T00:00:00</SentDateTime>
</SMSResponse>"""
タグの値を解析して取得しようとしています-Cancelled、MessageId、SMSErrorなど。PythonのElementtreeライブラリを使用しています。これまでのところ、次のようなことを試しました-
root = ET.fromstring(xmlData)
print root.find('Sent') // gives None
for child in root:
print chil.find('MessageId') // also gives None
ただし、タグを印刷することはできます-
for child in root:
print child.tag
//child.tag for the tag Cancelled is - {http://example.com}Cancelled
とそれぞれの値 -
for child in root:
print child.text
どうすれば次のようなものを取得できますか-
print child.Queued // will print false
PHPのように、ルートでアクセスできます-
$xml = simplexml_load_string($data);
$status = $xml->SMSError;