我的XML字符串是 -
我正在尝试解析并获取标签的值 - Cancelled,MessageId,SMSError等。我正在使用Python的Elementtree库。到目前为止,我已经尝试过像这样的操作 -
和它们各自的值与 -
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;
xml.etree.ElementTree
,链接在这里:[http://docs.python.org/2/library/xml.etree.elementtree.html#module-xml.etree.ElementTree]。我认为你正在使用`lxml.etree`,就像@root正在使用的一样。 - Hussainxml.etree
,Python 2.7。lxml
支持相同的API(虽然有一些改进,但在两者中都是相同的)。 - Martijn Pietersroot.find('n:Sent', namespaces=nsmap)
打印的是对象。在后面添加.text
即可。 - Hussain