我已经被困在这个问题上几天了:
我有一个类似于此的XML文件(包含100个条目)
if(lastposition==0 && ran==false)
{
callsomemethod(param);
}
我当前的代码正在尝试解析此xml文件:
<?xml version='1.0' encoding='us-ascii'?>
<content>
<email a="1" b="0">somename@somedomain.com</email>
<email a="0" b="1">otherdomain@somedomain.org</email>
</content>
现在,我得到以下输出:
from xml.dom import minidom
xmldoc = minidom.parse("data.xml")
content = xmldoc.getElementsByTagName("content")
address = xmldoc.getElementsByTagName("email")
for addresses in address:
Allow = True
Block = True
addressName = xmldoc.getElementsByTagName("email")
getB = addresses.attributes["b"]
b = getB.value
getA = addresses.attributes["a"]
a= getA.value
#setting allow and block list values
if (a == "1"):
Allow = True
print("This is allowed.")
elif (b == "1"):
Block = True
print("No, you cannot do that")
我的预期/希望结果是:
<DOM Element: addr at 0x3102850>
This is allowed.
<DOM Element: addr at 0x3102850>
No, you cannot do that
如果有人能指出我正确的方向,那将是美好的。我仍然是编程的初学者,现在有点卡住了。如果格式不正确,我也很抱歉,这是我第一次发帖。
谢谢!
答案 0 :(得分:0)
我猜你试图在你没有展示的某个阶段打印addressName。它是一个NodeList,所以你可以尝试
print (addressName[0].firstChild.nodeValue)
但是你已经在地址中拥有了Node,所以你可以
print (addresses.firstChild.nodeValue)
剥离它:
from xml.dom import minidom
xmldoc = minidom.parse("data.xml")
address = xmldoc.getElementsByTagName("email")
for addresses in address:
Allow = True
Block = True
b = addresses.attributes["b"].value
a = addresses.attributes["a"].value
#setting allow and block list values
print (addresses.firstChild.nodeValue)
if (a == "1"):
Allow = True
print("This is allowed.")
elif (b == "1"):
Block = True
print("No, you cannot do that")
但是,您可能在不同的XML中有多个文本元素,因此您可能需要使用:
print (" ".join(t.nodeValue for t in addresses.childNodes if t.nodeType == t.TEXT_NODE))
(当你使用地址时也使用地址,反之亦然,但这并不会导致问题更难阅读)