如何在xml文件中查找/替换多个字符串?

时间:2013-03-17 01:57:08

标签: xml excel notepad++ replace

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

谢谢

1 个答案:

答案 0 :(得分:3)

您可以使用一些简单的VBA来完成Excel s / s中的工作。通过传入查找替换范围来调用以下Sub,例如从Excel VBA立即窗口(使用Alt + F11访问VBA编辑器然后查看 - >立即):

ReplaceXML Range("A1:B600")

假设A1:B600包含600个find-replace字符串。

模块中定义以下内容后(在VBA编辑器中插入 - >模块(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
相关问题