0

这是样本数据。

输入.xml

<root>
    <entry id="1">
    <headword>go</headword>
    <example>I <hw>go</hw> to school.</example>
</entry>
</root>

我想将节点及其后代放入 . 那是,

输出.xml

<root>
    <entry id="1">
    <headword>go</headword>
            <examplegrp>
                <example>I <hw>go</hw> to school.</example>
            </examplegrp>
</entry>
</root>

我可怜且不完整的脚本是:

import codecs
import xml.etree.ElementTree as ET

fin = codecs.open(r'input.xml', 'rb', encoding='utf-8')

data = ET.parse(fin)
root = data.getroot()

example = root.find('.//example')
for elem in example.iter():
    ---and then I don't know what to do---
4

2 回答 2

0

http://docs.python.org/3/library/xml.dom.html?highlight=xml#node-objects http://docs.python.org/3/library/xml.dom.html?highlight=xml #文档对象

您可能希望遵循一些创建文档元素并将到达结果附加到它的范例。

group = Document.createElement(tagName)
for found in founds:
    group.appendNode(found)

或类似的东西

于 2013-01-29T12:06:30.957 回答
0

这是如何完成的示例:

text = """
<root>
    <entry id="1">
        <headword>go</headword>
        <example>I <hw>go</hw> to school.</example>
    </entry>
</root>
"""

import lxml.etree
import StringIO

data = lxml.etree.parse(StringIO.StringIO(text))
root = data.getroot()

for entry in root.xpath('//example/ancestor::entry[1]'):
    examplegrp = lxml.etree.SubElement(entry,"examplegrp")
    nodes = [node for node in entry.xpath('./example')]
    for node in nodes:
        entry.remove(node)
        examplegrp.append(node)

print lxml.etree.tostring(root,pretty_print=True)

这将输出:

<root>
    <entry id="1">
        <headword>go</headword>
        <examplegrp><example>I <hw>go</hw> to school.</example>
    </examplegrp></entry>
</root>
于 2013-01-29T12:10:54.887 回答