[英]Get Element using elementtree python
My XML looks something like: (Sorry if duplicate question, but I'm not very experienced with XML so I have a bit trouble with the terminology 我的XML看起来像:(很抱歉,如果有重复的问题,但是我对XML的经验不是很丰富,所以我在术语上有点麻烦
<sometags>
<Value>
<Scalar unitGlobalDataRef="Unit_0" unit="None" xmlns="xxxxyyyy">20</Scalar>
</Value>
</sometags>
Using this code: 使用此代码:
element = ET.parse(fileName)
root = element.getroot()
for subelement in root:
if (subelement.tag == "{xxyy}Parameter"):
for value in subelement:
for subval in value:
#Here is where it prints
if (subval.tag == "{xxxxyyyy}Scalar"):
print subval.tag
print subval.text
print subval.tail
print subval.attrib
prints 版画
{xxxxyyyy}Scalar
0
{'unitGlobalDataRef': 'Unit_0', 'unit': 'None'}
How can I get the value 20 out from the element? 如何从元素中获取值20?
subval.text should containt the information you're seeking. subval.text应该包含您要查找的信息。 Since you're getting a 0, it implies that your iterations may be wrong, and you're getting a different element than you think you are.
由于得到的是0,这意味着您的迭代可能是错误的,并且得到的元素与您认为的不同。 This is further reinforced by the fact that your attributes don't match.
您的属性不匹配的事实进一步加强了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.