Android 如何使用python将元素从一个xml复制到另一个xml
我有2个XML(它们恰好是android文本资源),第一个是:Android 如何使用python将元素从一个xml复制到另一个xml,android,python,xml,android-resources,Android,Python,Xml,Android Resources,我有2个XML(它们恰好是android文本资源),第一个是: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="TXT_T1">AAAA</string> </resources> AAAA 第二个是 <?xml version="1.0" encoding="utf-8"?> <resources> <stri
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="TXT_T1">AAAA</string>
</resources>
AAAA
第二个是
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="TXT_T2">BBBB</string>
</resources>
BBBB
我知道要复制的元素的属性,在我的示例中是TXT_T1。使用python,如何将其复制到其他xml并粘贴到TXT_T2后面?这只适用于您的简单示例:
>>> 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>
>从xml.dom.minidom导入解析字符串,文档
>>>def merge_xml(dom1、dom2):
节点\u到\u添加=无
dom3=文档()
对于dom1.getElementsByTagName('resources')中的节点_res:
对于node_res.getElementsByTagName('string')中的node_str:
如果“TXT\U T1”==节点\u str.attributes.values()[0]。值:
node_to_add=node_str
打破
对于dom2.getElementsByTagName('resources')中的node_res:
node_str3=dom3.appendChild(node_res)
对于node_res.getElementsByTagName('string')中的node_str:
node_str3.appendChild(node_str)
如果节点\u str.attributes.values()[0]中的“TXT\u T2”。值和要添加的节点\u不是无:
node_str3.appendChild(node_to_add)
返回dom3.toxml()
>>>dom2=parseString(“”)
BBBB
''')
>>>dom1=parseString(“”)
AAAA
''')
>>>打印合并xml(dom1、dom2)
BBAAAA
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()获取父项。除了添加的行没有缩进之外,它几乎可以完美地工作。我更新了帖子。您需要中断xml文件的格式,您可以通过创建解析器并将其设置为删除空白文本来完成此操作。在解析xml文件时,请记住传递解析器。原始代码仍然有效,文档的结构仍然有效。但是,如果您感到困扰,新代码也会处理格式问题。