2017-10-27 2 views
0

Ich habe Probleme, meine Xml in Python auszudrucken. Ich bin unsicher, wie man die richtigen Knoten zum Lesen bekommt. Wenn ich renne, was ich gerade habe, wird überhaupt nichts ausgedruckt. Ich bekomme keine Fehler, nur nichts drucken.Drucken xml-Knoten in Python

xml Skript

<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-Code:

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

Antwort

0

Ich frage mich, warum Sie zusätzliche Registerkarte haben in allen Codes.

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

Ich habe den Code nicht einmal gelesen, und ich bin sicher, dass dies zumindest einige Fehler geben, wenn die Logik falsch ist.

den Code einrücken wird in diesem Fall helfen