- ·上一篇文章:Python3 多线程
- ·下一篇文章:Python3 JSON 数据解析
Python3 XML 解析
码执行结果如下:*****Movie*****Title:
Enemy BehindType:
War, ThrillerFormat:
DVDYear:
2003Rating:
PGStars:
10Description:
Talk about a US-Japan war*****Movie*****Title:
TransformersType:
Anime, Science FictionFormat:
DVDYear:
1989Rating:
RStars:
8Description:
A schientific fiction*****Movie*****Title:
TrigunType:
Anime, ActionFormat:
DVDRating:
PGStars:
10Description:
Vash the Stampede!*****Movie*****Title:
IshtarType:
ComedyFormat:
VHSRating:
PGStars:
2Description:
Viewable boredom完整的 SAX API 文档请查阅Python SAX APIs使用xml.dom解析xml文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口。一个 DOM 的解析器在解析一个 XML 文档时,一次性读取整个文档,把文档中所有元素保存在内存中的一个树结构里,之后你可以利用DOM 提供的不同的函数来读取或修改文档的内容和结构,也可以把修改过的内容写入xml文件。 python中用xml.dom.minidom来解析xml文件,实例如下:
#!/usr/bin/python3from xml.dom.minidom import parseimport xml.dom.minidom
# 使用minidom解析器打开 XML 文档DOMTree = xml.dom.minidom.parse("movies.xml")collection = DOMTree.documentElementif collection.hasAttribute("shelf"):
print ("Root element :
%s" % collection.getAttribute("shelf"))
# 在集合中获取所有电影movies = collection.getElementsByTagName("movie")
# 打印每部电影的详细信息for movie in movies:
print ("*****Movie*****") if movie.hasAttribute("title"):
print ("Title:
%s" % movie.getAttribute("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)以上程序执行结果如下:Root element :
New Arrivals*****Movie*****Title:
Enemy BehindType:
War, ThrillerFormat:
DVDRating:
PGDescription:
Talk about a US-Japan war*****Movie*****Title:
TransformersType:
Anime, Science FictionFormat:
DVDRating:
RDescription:
A schientific fiction*****Movie*****Title:
TrigunType:
Anime, ActionFormat:
DVDRating:
PGDescription:
Vash the Stampede!*****Movie*****Title:
IshtarType:
ComedyFormat:
VHSRating:
PGDescription:
Viewable boredom完整的 DOM API 文档请查阅Python DOM APIs。
Enemy BehindType:
War, ThrillerFormat:
DVDYear:
2003Rating:
PGStars:
10Description:
Talk about a US-Japan war*****Movie*****Title:
TransformersType:
Anime, Science FictionFormat:
DVDYear:
1989Rating:
RStars:
8Description:
A schientific fiction*****Movie*****Title:
TrigunType:
Anime, ActionFormat:
DVDRating:
PGStars:
10Description:
Vash the Stampede!*****Movie*****Title:
IshtarType:
ComedyFormat:
VHSRating:
PGStars:
2Description:
Viewable boredom完整的 SAX API 文档请查阅Python SAX APIs使用xml.dom解析xml文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口。一个 DOM 的解析器在解析一个 XML 文档时,一次性读取整个文档,把文档中所有元素保存在内存中的一个树结构里,之后你可以利用DOM 提供的不同的函数来读取或修改文档的内容和结构,也可以把修改过的内容写入xml文件。 python中用xml.dom.minidom来解析xml文件,实例如下:
#!/usr/bin/python3from xml.dom.minidom import parseimport xml.dom.minidom
# 使用minidom解析器打开 XML 文档DOMTree = xml.dom.minidom.parse("movies.xml")collection = DOMTree.documentElementif collection.hasAttribute("shelf"):
print ("Root element :
%s" % collection.getAttribute("shelf"))
# 在集合中获取所有电影movies = collection.getElementsByTagName("movie")
# 打印每部电影的详细信息for movie in movies:
print ("*****Movie*****") if movie.hasAttribute("title"):
print ("Title:
%s" % movie.getAttribute("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)以上程序执行结果如下:Root element :
New Arrivals*****Movie*****Title:
Enemy BehindType:
War, ThrillerFormat:
DVDRating:
PGDescription:
Talk about a US-Japan war*****Movie*****Title:
TransformersType:
Anime, Science FictionFormat:
DVDRating:
RDescription:
A schientific fiction*****Movie*****Title:
TrigunType:
Anime, ActionFormat:
DVDRating:
PGDescription:
Vash the Stampede!*****Movie*****Title:
IshtarType:
ComedyFormat:
VHSRating:
PGDescription:
Viewable boredom完整的 DOM API 文档请查阅Python DOM APIs。
Python3 XML 解析