Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Python解析XML标记并使用XML.dom.minidom替换_Python_Xml_Xml Parsing - Fatal编程技术网

通过Python解析XML标记并使用XML.dom.minidom替换

通过Python解析XML标记并使用XML.dom.minidom替换,python,xml,xml-parsing,Python,Xml,Xml Parsing,我的XML文件test.XML包含以下标记 <?xml version="1.0" encoding="ISO-8859-1"?> <AppName> <out>This is a sample output with <test>default</test> text </out> <AppName> 这是一个带有默认文本的示例输出 到目前为止,我已经编写了一个python代码,该代码执行以下操作

我的XML文件test.XML包含以下标记

<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
    <out>This is a sample output with <test>default</test> text </out>
<AppName>

这是一个带有默认文本的示例输出
到目前为止,我已经编写了一个python代码,该代码执行以下操作:

from xml.dom.minidom import parseString
list = {'test':'example'}
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
if (len(dom.getElementsByTagName('out'))!=0):
    xmlTag = dom.getElementsByTagName('out')[0].toxml()
    out = xmlTag.replace('<out>','').replace('</out>','')
    print out
从xml.dom.minidom导入解析字符串
列表={'test':'example'}
file=open('test.xml','r')
data=file.read()
file.close()文件
dom=parseString(数据)
如果(len(dom.getElementsByTagName('out'))!=0):
xmlTag=dom.getElementsByTagName('out')[0].toxml()
out=xmlTag.replace(“”,“”),replace(“”,“”)
打印出来
以下程序的输出为
这是一个带有默认文本的示例输出

您还将注意到,我有一个定义了
list={'test':'example'}
的列表

我想检查是否在列表中列出了一个标记,该标记将替换为相应的值,否则为默认值

在这种情况下,输出应为:


这是一个带有示例文本的示例输出

这将或多或少满足您的要求:

from xml.dom.minidom import parseString, getDOMImplementation

test_xml = '''<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
    <out>This is a sample output with <test>default</test> text </out>
</AppName>'''

replacements = {'test':'example'}
dom = parseString(test_xml)
if (len(dom.getElementsByTagName('out'))!=0):
    xmlTag = dom.getElementsByTagName('out')[0]
    children =  xmlTag.childNodes
    text = ""
    for c in children:
        if c.nodeType == c.TEXT_NODE:
            text += c.data
        else:
            if c.nodeName in replacements.keys():
                text += replacements[c.nodeName]
            else: # not text, nor a listed tag
                text += c.toxml()
    print text
从xml.dom.minidom导入解析字符串,getDOMImplementation
测试xml=“”
这是一个带有默认文本的示例输出
'''
替换={'test':'example'}
dom=parseString(测试xml)
如果(len(dom.getElementsByTagName('out'))!=0):
xmlTag=dom.getElementsByTagName('out')[0]
children=xmlTag.childNodes
text=“”
对于儿童中的c:
如果c.nodeType==c.TEXT\u节点:
text+=c.data
其他:
如果在replacements.keys()中使用c.nodeName:
text+=替换项[c.nodeName]
else:#不是文本,也不是列出的标记
text+=c.toxml()
打印文本
请注意,我使用了
replacements
而不是
list
。用python术语来说,它是一个字典,而不是一个列表,所以这是一个令人困惑的名称。它也是一个内置函数,因此应该避免将其用作名称

如果您想要一个dom对象而不仅仅是文本,那么需要采取不同的方法