【发布时间】:2016-03-01 11:17:27
【问题描述】:
我最近开始研究 python。我正在尝试解析 xml 文档。考虑以下 xml 文件以供参考:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
在这里我想检索第一个 book 标记及其所有内容,即
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
我来自 scala 背景,在那里我可以轻松做到这一点
val node = scala.xml.XML.loadString(str)
val nodeSeq = node \\ "book"
nodeSeq.head.toString()
我曾尝试使用lxml 和xpath 来执行此操作,但它会变得复杂(递归地获取嵌套元素的内容)以实现上述要求。在python中有没有简单的方法来做到这一点?也可以扩展为html吗?
TIA
【问题讨论】:
-
您是否尝试过使用 minidom,对于有 Scala 或 Java 背景的人来说,它可能是最简单的软件包。
标签: python xml xml-parsing html-parsing