2017-10-27 16 views
0

私のXMLをpythonで印刷するのに問題があります。私は正しいノードを読む方法を確信しています。私が現在実行しているものは、何もプリントアウトしていません。私は何のエラーもなく、何も印刷しません。Pythonでxmlノードを印刷する

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

あなたはすべてのコード内の余分なタブを持っている理由だろうか。

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)) 

私もあなたのコードを読んでいないと私はロジックが間違っている場合、これは少なくともあなたにいくつかのエラーを与えることを確信しています。

コードをインデントすると、この場合に役立ちます