如何在xml文件中查找/替换多个字符串? [英] How can I Find/Replace multiple strings in an xml file?

查看:186
本文介绍了如何在xml文件中查找/替换多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在XML文件中替换大约600个不同的文本字符串(我正在使用notepad ++,但如果可以完成任务,我也可以使用其他程序).文本更改在单独的excel文件中列出.有没有一种方法可以运行脚本或命令来一次查找/替换所有字符串,而不必单独执行每个字符串?

I have about 600 different text strings that I need to replace within an XML file (I am using notepad++ though I can use other programs too if this would accomplish the task). The text changes are listed in a separate excel file. Is there a way that I can run a script or command to find/replace all of the strings in one shot, without having to do each one individually?

谢谢

推荐答案

您可以使用一些简单的VBA从Excel的内部完成这项工作.通过传递查找替换范围来调用下面的Sub,例如从Excel VBA Instant窗口中(使用Alt + F11进行VBA编辑器访问,然后单击View-> Instant):

You could use some simple VBA to do the job from within your Excel s/s. Call the below Sub by passing in the find-replace range e.g. from the Excel VBA Immediate window (access using Alt+F11 for the VBA editor then View -> Immediate):

ReplaceXML Range("A1:B600")

假设A1:B600包含600个查找替换字符串.

Assuming A1:B600 contains the 600 find-replace strings.

模块中定义以下内容(从VBA编辑器(Alt + F11)中插入->模块):

After defining the below in a module (Insert -> Module from within the VBA editor (Alt+F11) ):

Option Explicit ' Use this !

Public Sub ReplaceXML(rFindReplaceRange as Range) ' Pass in the find-replace range

    Dim sBuf As String
    Dim sTemp As String
    Dim iFileNum As Integer
    Dim sFileName As String
    Dim i as Long

    ' Edit as needed
    sFileName = "C:\filepath\filename.xml"

    iFileNum = FreeFile
    Open sFileName For Input As iFileNum

    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        sTemp = sTemp & sBuf & vbCrLf
    Loop

    Close iFileNum

    ' Loop over the replacements
    For i = 1 To rFindReplaceRange.Rows.Count
        If rFindReplaceRange.Cells(i, 1) <> "" Then
            sTemp = Replace(sTemp, rFindReplaceRange.Cells(i, 1), rFindReplaceRange(i, 2))
        End If
    Next i

    ' Save file

    iFileNum = FreeFile

    ' Alter sFileName first to save to a different file e.g.
    sFileName = "C:\newfilepath\newfilename.xml"
    Open sFileName For Output As iFileNum

    Print #iFileNum, sTemp

    Close iFileNum

End Sub

这篇关于如何在xml文件中查找/替换多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆