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最近:
你好尼克,我注意到,它並沒有打印出蟒蛇外殼答案。只是一個空白頁面。 – user3346746
@ user3346746謝謝,修正。現在應該在示例XML上工作。 –