0

我有 2 个 xml(它们恰好是 android 文本资源),第一个是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="TXT_T1">AAAA</string>
</resources>

第二个是

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="TXT_T2">BBBB</string>
</resources>

我知道要复制的元素的属性,在我的示例中是 TXT_T1。使用python,如何将其复制到其他xml并将其粘贴到TXT_T2后面?

4

2 回答 2

2

lxml 是 xml 解析之王。我不确定这是否是你要找的,但你可以尝试这样的事情

from lxml import etree as et

# select a parser and make it remove whitespace
# to discard xml file formatting
parser = et.XMLParser(remove_blank_text=True)

# get the element tree of both of the files
src_tree = et.parse('src.xml', parser)
dest_tree = et.parse('dest.xml', parser)

# get the root element "resources" as
# we want to add it a new element
dest_root = dest_tree.getroot()

# from anywhere in the source document find the "string" tag
# that has a "name" attribute with the value of "TXT_T1"
src_tag = src_tree.find('//string[@name="TXT_T1"]')

# append the tag
dest_root.append(src_tag)

# overwrite the xml file
et.ElementTree(dest_root).write('dest.xml', pretty_print=True, encoding='utf-8', xml_declaration=True)

这假定第一个文件称为 src.xml,第二个文件称为 dest.xml。这也假设您需要在其下复制新元素的元素是父元素。如果没有,您可以使用 find 方法找到您需要的父级,或者如果您不知道父级,则搜索带有 'TXT_T2' 的标签并使用 tag.getparent() 获取父级。

于 2013-08-28T15:33:35.293 回答
1

这仅适用于您的简单示例:

>>> from xml.dom.minidom import parseString, Document
>>> def merge_xml(dom1, dom2):
    node_to_add = None
    dom3 = Document()
    for node_res in dom1.getElementsByTagName('resources'):
        for node_str in node_res.getElementsByTagName('string'):
            if 'TXT_T1' == node_str.attributes.values()[0].value:
                node_to_add = node_str
                break

    for node_res in dom2.getElementsByTagName('resources'):
        node_str3 = dom3.appendChild(node_res)
        for node_str in node_res.getElementsByTagName('string'):
            node_str3.appendChild(node_str)
            if 'TXT_T2' in node_str.attributes.values()[0].value and node_to_add is not None:
                node_str3.appendChild(node_to_add)
    return dom3.toxml()

>>> dom2 = parseString('''<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="TXT_T2">BBBB</string>
</resources>''')
>>> dom1 = parseString('''<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="TXT_T1">AAAA</string>
</resources>''')
>>> print merge_xml(dom1, dom2)
<?xml version="1.0" ?><resources>

<string name="TXT_T2">BBBB</string><string name="TXT_T1">AAAA</string></resources>
于 2013-08-28T15:10:48.070 回答