使用 minidom 解析 XML 个属性

Parsing XML attributes with minidom

我想用 Python minidom 解析 XML 文件的内容。 假设我有一个这样的 xml 文档:

<?xml version="1.0" ?>

<library>
 <book>
  <title>Sandman Volume 1: Preludes and Nocturnes</title>
  <author>Neil Gaiman</author>
 </book>
 <book owner="jim" type="fiction">
  <title>Good Omens</title>
  <author>Neil Gamain</author>
  <author>Terry Pratchett</author>
  </book>
 <book owner = "john" type="fiction">
  <title>All the Lovely Things</title>
  <author>John Wize</author>
 </book>
 <book owner="john" type="non-fiction">
  <title>Beginning Python</title>
  <author>Peter Norton, et al</author>
 </book>
<book owner="john" type="fiction">
  <title>Beginning Python</title>
  <author>Peter Norton, et al</author>
 </book>
</library>

我想解析文档并将所有者“john”的所有书名输出到文件,但不包括 owner =“john”的书名。

到目前为止我的脚本:

from xml.dom.minidom import parse
import xml.dom.minidom
import csv
from itertools import count

# Declare Counter
c = count(1)

def writeToCSV(myLibrary):
    with open('csvout.csv', 'wb') as csvfile:
        writer = csv.writer(csvfile, delimiter = ',')
        writer.writerow(['key', 'title', 'author', 'author']) #added key
        books = myLibrary.getElementsByTagName("book")
        for book in books:
            titleValue = book.getElementsByTagName("title")[0].childNodes[0].data
            authors =[]
            for author in book.getElementsByTagName("author"):
                authors.append(author.childNodes[0].data)
            writer.writerow([c.next()] + [titleValue] + authors)

# Create XML objects            
doc = parse('library.xml')
myLibrary = doc.getElementsByTagName("library")[0]

# Call function
writeToCSV(myLibrary)

我的脚本是在我添加书籍属性之前编写的。我的输出是:

key,title,author,author
1,Sandman Volume 1: Preludes and Nocturnes,Neil Gaiman
2,Good Omens,Neil Gamain,Terry Pratchett
3,All the Lovely Things,John Wize
4,Beginning Python,"Peter Norton, et al"

如果我只想显示属性为“john”和“fiction”的书籍,我该怎么做?

一种解决方案是检查属性 owner,如下所示:if book.getAttribute('owner') == 'james':.

from xml.dom.minidom import parse
import xml.dom.minidom
import csv
from itertools import count

# Declare Counter
c = count(1)

def writeToCSV(myLibrary):
    with open('csvout.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter = ',')
    writer.writerow(['key', 'title', 'author', 'author']) #added key
    books = myLibrary.getElementsByTagName("book")
    for book in books:
        if book.getAttribute('owner') == 'james':
            titleValue = book.getElementsByTagName("title")[0].childNodes[0].data
            authors =[]
            for author in book.getElementsByTagName("author"):
                authors.append(author.childNodes[0].data)
            writer.writerow([c.next()] + [titleValue] + authors)

# Create XML objects            
doc = parse('library3.xml')
myLibrary = doc.getElementsByTagName("library")[0]

# Call function
writeToCSV(myLibrary)