我无法让我的xml在python中打印出来。我不确定如何获取正确的节点。当我运行我目前没有什么打印输出。我没有错误,只是没有打印。
xml脚本
<NEWS>
<ARTICLE>
<TITLE>Mark Mulder ends 2015 comeback bid</TITLE>
<AUTHOR>Igor</AUTHOR>
<PUBLIC>T</PUBLIC>
<CONTENT>
38-year-old left-hander Mark Mulder will not be attempting another comeback this tear, according to Jerry Crasnick of ESPN and Baseball America. Mulder tried to return from a lengthy hiatus last year, but a comeback bid ended with a spring training injury in Angels camp last year. "I just couldn't get where I needed to be," Mulder said. Mulder went 103-60 with a 4.18 ERA in nine years in the majors. He hasn't pitched in an MLB game since 2008.
</CONTENT>
</ARTICLE>
<NEWS>
python代码:
import xml.dom.minidom as minidom
document = minidom.parse("cgi-bin/news.xml")
page = document.childNodes[0]
print "<html>"
print "<head><title>NEW News Articles List</title></head>"
print "<body>"
print "<h2>Welcome to New News Inc.!<br> You are currently a GUEST here.</h2>"
print """\
<form method="GET" >
<input type="submit" value="Submit" name="loginPageCall" >
</form>
"""
for child in page.childNodes:
if child.nodeType == minidom.Node.TEXT_NODE:
sys.stdout.write(child.data)
elif child.nodeType == minidom.Node.ELEMENT_NODE:
if child.nodeName == "TITLE":
sys.stdout.write('<a href="%s">%s</a>' % (child.getAttribute("url"), child.childNodes[0].data))
答案 0 :(得分:0)
我想知道你为什么在所有代码中都有额外的标签。
import xml.dom.minidom as minidom
document = minidom.parse("cgi-bin/news.xml")
page = document.childNodes[0]
print "<html>"
print "<head><title>NEW News Articles List</title></head>"
print "<body>"
print "<h2>Welcome to New News Inc.!<br> You are currently a GUEST here.</h2>"
print """\
<form method="GET" >
<input type="submit" value="Submit" name="loginPageCall" >
</form>
"""
for child in page.childNodes:
if child.nodeType == minidom.Node.TEXT_NODE:
sys.stdout.write(child.data)
elif child.nodeType == minidom.Node.ELEMENT_NODE:
if child.nodeName == "TITLE":
sys.stdout.write('<a href="%s">%s</a>' % (child.getAttribute("url"), child.childNodes[0].data))
我甚至没有读过您的代码,我相信如果逻辑错误,这至少会给您一些错误。
在这种情况下缩进代码会有所帮助