我正在尝试使用xml.etree.ElementTree解析和读取xml(我无法移动到lxml),但我一直无法使用。
XML:https://pastebin.com/yJqAW0L0
<GetResponse xmlns="http://mywebsite.com/myservice/">
<AutoScalingGroup>
<AutoScalingGroupName>foo</AutoScalingGroupName>
<AttributeValuePair>
<Attribute>owner</Attribute>
<Value>bob</Value>
</AttributeValuePair>
</AutoScalingGroup>
</GetResponse>
我已经尝试过了
import xml.etree.ElementTree as ET
ET.register_namespace('', "http://mywebsite.com/myservice/")
NSMAP = {'service':'http://mywebsite.com/myservice/'}
tree = ET.fromstring(page) # This is where i grab the xml from
autoscalingGroups = tree.findall('.//service:AutoScalingGroup', namespaces = NSMAP)
for asg in autoscalingGroups:
name = asg.findtext('.//service:AutoScalingGroupName', namespaces = NSMAP, default = "Default asg name")
print "asg name: " + str(name)
这不会返回任何内容,我很难找到原因。
1)我如何得到'foo'? 2)我怎么得到'鲍勃'? 我使用错误的XML xpath吗?