2015-05-07 51 views
0

我使用本教程这个例子尝试(这里的a link):怎样才能读取XML项

#!/usr/bin/python 

import xml.sax 

class MovieHandler(xml.sax.ContentHandler): 
    code........ 
if (__name__ == "__main__"): 

    # create an XMLReader 
    parser = xml.sax.make_parser() 
    # turn off namepsaces 
    parser.setFeature(xml.sax.handler.feature_namespaces, 0) 

    # override the default ContextHandler 
    Handler = MovieHandler() 
    parser.setContentHandler(Handler) 

    parser.parse("movies.xml") 

这给这个结果作为输出:

*****Movie***** 
Title: Enemy Behind 
Type: War, Thriller 
Format: DVD 
Year: 2003 
Rating: PG 
Stars: 10 
Description: Talk about a US-Japan war 
*****Movie***** 
Title: Transformers 
Type: Anime, Science Fiction 
Format: DVD 
Year: 1989 
Rating: R 
Stars: 8 
Description: A schientific fiction 
*****Movie***** 
Title: Trigun 
Type: Anime, Action 
Format: DVD 
Rating: PG 
Stars: 10 
Description: Vash the Stampede! 
*****Movie***** 
Title: Ishtar 
Type: Comedy 
Format: VHS 
Rating: PG 
Stars: 2 
Description: Viewable boredom 

假如我只希望这导致:

*****Movie***** 
Title: Enemy Behind 
Type: War, Thriller 
Format: DVD 
Year: 2003 
Rating: PG 
Stars: 10 

或本

****Movie***** 
    Title: Enemy Behind 
    Type: War, Thriller 
    Rating: PG 
    Stars: 10 

我能做些什么不同吗?我刚刚开始学习python & XML最近:

回答

1

这种事情可以通过解析XML来创建一个DOM树来完成,那么你可以很容易地随机查询。

例如,要打印影片标题为“敌人,后面”你可以做这样的事情:

#!/usr/bin/python 

from xml.dom.minidom import parse 
import xml.dom.minidom 

# Open XML document using minidom parser 
DOMTree = xml.dom.minidom.parse("movies.xml") 
collection = DOMTree.documentElement 
if collection.hasAttribute("shelf"): 
    print "Root element : %s" % collection.getAttribute("shelf") 

# Get all the movies in the collection 
movies = collection.getElementsByTagName("movie") 

# Print detail of each movie. 
for movie in movies: 
    title = movie.getAttribute("title") 
    if title == "Enemy Behind": 
     print "*****Movie*****" 
     print "Title: %s" % title 

     type = movie.getElementsByTagName('type')[0] 
     print "Type: %s" % type.childNodes[0].data 
     format = movie.getElementsByTagName('format')[0] 
     print "Format: %s" % format.childNodes[0].data 
     rating = movie.getElementsByTagName('rating')[0] 
     print "Rating: %s" % rating.childNodes[0].data 
     description = movie.getElementsByTagName('description')[0] 
     print "Description: %s" % description.childNodes[0].data 
+0

你好尼克,我注意到,它并没有打印出蟒蛇外壳答案。只是一个空白页面。 – user3346746

+0

@ user3346746谢谢,修正。现在应该在示例XML上工作。 –