0

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<raml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="raml21.xsd">
<cmData type="actual" scope="all" name="plan_file">
<header>
 <log dateTime="2011-05-18T04:05:02" action="created" /> 
</header>
<managedObject class="Fruit">
 <p name="Apple">100</p> 
 <p name="Mango">4</p> 
 <p name="Pear">99</p> 
 <p name="Jackfruit">67</p> 
 <p name="Strawberry">200</p> 
 <p name="Guava">100</p> 
 <p name="Banana">100</p> 
 <p name="Breadfruit">1500</p> 
 <p name="Musambi">100</p> 
</managedObject>
</cmData>
</raml>

我需要做的是。我需要用另一个数字替换给定属性的文本节点(100、4、99)(在运行时通过 python shell 输入)。我一次只需要更改一个文本节点(也通过贝壳)。我需要一个带有更改值的单独 xml 文件。

我写了这样的python脚本:

from xml.dom import minidom
import os.path

def new_value(parameter, parameter_value, target_dir, inputfile):    
    count = len(open(inputfile).readlines())

    dom = minidom.parse(inputfile)
    name = dom.getElementsByTagName('p')
    inFile = open(inputfile,'r')

    fullname = os.path.join(target_dir, "test" + str(parameter_value) + ".xml")
    outFile = open(fullname,'w')

    for i in range(count):
        content = inFile.readline()
        matchobj = re.search(parameter, content)

        if(matchobj):        
            newcontent = content.replace(name[2].firstChild.nodeValue, str(parameter_value))
            outFile.write(newcontent)

        else:
            outFile.write(content)
    outFile.close()


parameter = input("Enter the parameter: ")
target_dir = input("Enter the target directory: ")
input_file = input("Enter the input file: ")
parameter_value = input("Enter the value to replace: ")
new_value(parameter, parameter_value, target_dir, input_file)

在这里,因为我使用的是表达式,

newcontent = content.replace(name[2].firstChild.nodeValue, str(parameter_value))

此脚本正在运行但使用此脚本(因为我使用的是名称 [2]),我只能更改 xml 文件的索引 2,即 Pear。如果我写的是 1 而不是 2,我可以更改芒果等。但我需要使脚本通用。我该如何修改脚本???

感谢您的帮助.. :)

4

1 回答 1

0

我注意到您使用的是 Python 3 ( input()) 而不是 Python 2 ( raw_input())。

您究竟为什么要尝试使用正则表达式(re模块)?据我了解,您正在尝试将 XML 文件解析为 DOM,找到<p>由其name属性标识的元素,将其文本替换为用户提供的文本,然后将其写入新文件。与尝试在原始 XML 流上运行正则表达式相比,您可以通过操作 DOM 更可靠地进行替换。以下 Python 3 程序展示了如何操作 DOM:

#!/usr/bin/env python3
from xml.dom import minidom
import os.path

def new_value(parameter, parameter_value, target_dir, inputfile):
    # Load the XML file and parse it
    dom = minidom.parse(inputfile)

    # Find all <p> elements with the correct name= attribute
    els = [element
           for element in dom.getElementsByTagName('p')
           if element.getAttribute('name') == parameter]

    # Choose the first among these
    chosen_el = els[0]

    # Set its text content
    if chosen_el.hasChildNodes:
        # Assumes that the first child is in fact a text node
        chosen_el.firstChild.nodeValue = parameter_value
    else:
        # If the element is empty, add a child node
        chosen_el.appendChild(dom.createTextNode(parameter_value))

    fullname = os.path.join(target_dir, "test" + str(parameter_value) + ".xml")

    # Opening a file using a "with open" block automatically
    # closes it at the end of the block
    with open(fullname, 'wb') as outFile:
        outFile.write(dom.toxml('utf-8'))

parameter = input("Enter the parameter: ")              # or 'Mango'
target_dir = input("Enter the target directory: ")      # or '.'
input_file = input("Enter the input file: ")            # or 'so_xml.xml'
parameter_value = input("Enter the value to replace: ") # or 'Manstop'
new_value(parameter, parameter_value, target_dir, input_file)
于 2013-09-05T02:07:16.127 回答