您的第一个问题是访问该文件。由于 VBScript/FileSystemObject 不支持 UTF-8 读/写,您可能必须使用 ADODB.Stream。如果每一行都包含一个格式正确/有效的 XML 文档 -<Stuff> ... </stuff>
只是一个错字 - 您必须使用 .LoadXml 将字符串/行提供给 Msxml2.DOMDocument。然后您可能可以应用您的“更改节点”代码。
添加:
显示方法的概念证明代码:
Dim sIFSpec : sIFSpec = goFS.GetAbsolutePathName("..\data\01.xml")
Dim sOFSpec : sOFSpec = goFS.GetAbsolutePathName("..\data\01-1.xml")
Dim oXD : Set oXD = CreateObject("Msxml2.DOMDocument")
Dim sXPath : sXPath = "/stuff/name"
Dim aLines : aLines = Split(readAllFromFileUTF8(sIFSpec), vbCrLf)
WScript.Echo Join(aLines, vbCrLf)
Dim nLine, sLine, ndFnd
For nLine = 0 To UBound(aLines)
sLine = aLines(nLine)
If "" <> sLine Then
oXD.LoadXml sLine
Set ndFnd = oXD.selectSingleNode( sXPath )
If ndFnd Is Nothing Then
WScript.Echo "|", sXPath, "| not found"
Else
WScript.Echo "found |" & ndFnd.text & "|"
ndFnd.text = "Ekkehard Hörner"
aLines(nLine) = Replace(oXD.xml, vbCrLf, "")
End If
End If
Next
writeAllToFileUTF8 sOFSpec, Join(aLines, vbCrLf)
WScript.Echo Join(aLines, vbCrLf)
输出:
<?xml version="1.0" encoding="UTF-8"?> <stuff><num>1</num><name>Hörner</name></stuff>
<?xml version="1.0" encoding="UTF-8"?> <stuff><num>2</num><name>Hörner</name></stuff>
<?xml version="1.0" encoding="UTF-8"?> <stuff><num>3</num><name>Hörner</name></stuff>
found |Hörner|
found |Hörner|
found |Hörner|
<?xml version="1.0"?><stuff><num>1</num><name>Ekkehard Hörner</name></stuff>
<?xml version="1.0"?><stuff><num>2</num><name>Ekkehard Hörner</name></stuff>
<?xml version="1.0"?><stuff><num>3</num><name>Ekkehard Hörner</name></stuff>
FunctionreadAllFromFileUTF8
和 Sub 留作writeAllToFileUTF8
练习。