1

このような XML ファイル ( bookstore.xml ) があるとします。

<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

そして、 = JK Rowlingというbook要素を削除したいと思います。 このように著者に一致するすべての要素を取得できることを知っています(Jython)author

docFactory = DocumentBuilderFactory.newInstance()
docBuilder = docFactory.newDocumentBuilder()
doc = docBuilder.parse(bookstore.xml)
list = doc.getElementsByTagName("author")

変更した XML ツリーを bookstore.xml に書き込みたいと思います。

ありがとう !

4

3 に答える 3

1

org.w3c.dom.*およびjavax.xml.*Java API を使用する代わりに、ElementTreeを使用することをお勧めします。このライブラリは Jython でサポートされており、物事を大幅に簡素化します。

from xml.etree import ElementTree as ET

root = ET.parse("bookstore.xml").getroot()
books = root.findall("book")

for book in books:
    if book.findtext("author") == "J K. Rowling":
        print "Found!"
        root.remove(book)

ET.ElementTree(root).write("output.xml")

Jython 2.5.2 (および CPython 2.7.2) でテスト済み。

于 2013-01-09T20:15:04.460 に答える
0

以下はpython2.7での操作手順です。ただし、xml 構造に過度に依存するため、スクリプトには書きませんでした。

>>> from xml.dom import minidom
>>> xmldoc = minidom.parse('a.xml')
>>> root = xmldoc.documentElement
>>> nodeList = xmldoc.childNodes
>>> bookstore = nodeList[0].childNodes
>>> bookstore
[<DOM Text node "u'\n'">, <DOM Element: book at 0x2544580>, <DOM Text node "u'\n'">, <DOM Element: book at 0x2544a30>, <DOM Text node "u'\n'">, <DOM Element: book at x2544e90>, <DOM Text node "u'\n'" >, <DOM Element: book at 0x25475d0>, <DOM Text node "u'\n'">]
>>> bookstore[3].getElementsByTagName("author")[0].childNodes[0].data
u'J K. Rowling'
>>> nodeList[0].removeChild(bookstore[3])
>>> with open('output.xml', 'w') as f:
...     f.write(xmldoc.saveXML(nodeList[0]))
...
>>> 

結果:

<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

この dom moudle は非常に使いにくいと思います。Pythonのxml.etree.ElementTreeなど、他のものを試してみることをお勧めします。

于 2013-01-09T06:52:32.507 に答える
0

以下が機能しました

for i in range(list.getLength()):
    node = list.item(i)
    if node != None and node.getNodeName() == "book":
        children = node.getChildNodes()
        for j in range(children.getLength()):
            print "Looking for J K. Rowling in book"
            child = children.item(j)
            if  child.getNodeName() == "author" and child.getTextContent() == "J K. Rowling":
                print "************"
                print "Found!!!!!"
                print child.getNodeName()
                print node.getTextContent()
                node1= node.getParentNode().removeChild(child.getParentNode())
于 2013-01-09T17:50:25.333 に答える