将XML数据解析为TXT文件

时间:2016-11-09 19:09:51

标签: xml parsing vbscript

我有一个TXT和一个看起来像这样的XML:

TEMP-4.txt

1907|
233845|
4187|
4668|
5471|

VendorConfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<Vendors>
  <Vendor>
    <SourceID>210986|</SourceID>
    <SourceName>Rise Commercial Services|SPRING-330RAYFO</SourceName>
    <DestName>Rise Commercial Services|SPRING-330RAYFO</DestName>
  </Vendor>
  <Vendor>
    <SourceID>1907|</SourceID>
    <SourceName>Franklin Machine Products Inc|LUMBER-10MTHOL</SourceName>
    <DestName>Franklin Machine Products Inc1907|</DestName>
  </Vendor>
  <Vendor>
    <SourceID>233845|</SourceID>
    <SourceName>Coastline Electric Co Inc233845|</SourceName>
    <DestName>Coastline Electric Co Inc233845|</DestName>
  </Vendor>
</Vendors>

我需要循环遍历TXT文件中的每个订单项,并使用VBScript检查VendorConfig.xml中是否存在该订单项。如果它作为SourceID存在于XML中,我需要从XML附加其他信息。如果XML中不存在SourceID,我需要附加一条错误消息。到目前为止我看起来像这样,但XML解析器似乎没有返回任何匹配,我无法弄清楚原因:

VBSCRIPT

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("C:\myAbsolutePath\VendorConfig.xml")
If objXMLDoc.parseError.errorCode <> 0 Then
    MsgBox ("XML connection error detected.")
End If

Set Root = objXMLDoc.documentElement
Set NodeList = Root.getElementsByTagName("Vendor")

Dim AddVendorDetails, InputFile, FSO, oFile, strTempSourceIDs
InputFile = "C:\myAbsolutePath\temp-4.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(InputFile)
strTempSourceIDs = oFile.ReadAll
oFile.Close

Dim splitCounter
Dim splitCatcher
Dim IDtest

SplitCatcher = Split(strTempSourceIDs, vbCrLf)
For splitCounter = 0 To UBound(SplitCatcher)
    IDtest = SplitCatcher(splitCounter)
    For Each i In NodeList
        If IDtest = i.getElementsByTagName("SourceID")(0).nodeValue Then
            Dim IDnode
            Dim Sourcenode
            Dim Destnode
            Set SourceID = i.getElementsByTagName("SourceID")(0)
            Set SourceName = i.getElementsByTagName("SourceName")(0)
            Set DestName = i.getElementsByTagName("DestName")(0)

            If SourceID.nodeValue <> "" Then
                IDnode = SourceID.nodeValue
            Else
                IDnode = "ERROR:Undefined ID"
            End If
            If SourceName.nodeValue <> "" Then
                Sourcenode = SourceName.nodeValue
            Else
                Sourcenode = "(No Source Name in config files)"
            End If
            If DestName.nodeValue <> "" Then
                Destnode = DestName.nodeValue
            Else
                Destnode = "(No Dest Name in config files)"
            End If

            AddVendorDetails = IDnode & vbCrLf
            AddVendorDetails = AddVendorDetails & "SourceName: " & Sourcenode & vbCrLf
            AddVendorDetails = AddVendorDetails & "DestName: " & Destnode & vbCrLf
            If SourceID.nodeValue <> "" Then
                MsgBox (AddVendorDetails)
            End If
            strTempSourceIDs = Replace(strTempSourceIDs, IDnode, AddVendorDetails)
        End If
    Next
Next

strTempSourceIDs = "######################## " & WeekdayName(Weekday(Date)) & Date & " ########################" & vbCrLf & strTempSourceIDs
MsgBox (strTempSourceIDs)

2 个答案:

答案 0 :(得分:1)

正如您已经发现的那样,您需要检查的属性是text,而不是nodeValue。另外,我建议使用Msxml2.DOMDocumentSelectNodes使用XPath表达式而不是getElement*种方法,并弃用Microsoft.XMLDOM

Set objXMLDoc = CreateObject("Msxml2.DOMDocument.6.0")
objXMLDoc.Async = False
objXMLDoc.Load "C:\path\to\VendorConfig.xml"
...
For Each node In objXMLDoc.SelectNodes("//SourceID")
  ...
Next

要确保ID的唯一性,请将文件temp-4.txt加载到dictionary

listfile = "C:\path\to\temp-4.txt"

Set fso    = CreateObject("Scripting.FileSystemObject")
Set idList = CreateObject("Scripting.Dictionary")
For Each id In Split(fso.OpenTextFile(listfile).ReadAll, vbNewLine)
  idList(Trim(id)) = True
Next

使用XML中的ID以相同的方式更新列表:

For Each node In objXMLDoc.SelectNodes("//SourceID")
  idList(Trim(node.text)) = True
Next

然后将字典中的keys写回文件:

fso.OpenTextFile(listfile, 2).Write Join(idList.Keys, vbNewLine)

答案 1 :(得分:0)

getElementsByTagName不起作用,因此IDtest没有获得SourceID的值。

如果我这样编码,它就可以了。

    Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load("C:\users\jdarling\20161109\01\VendorConfig.xml")
If objXMLDoc.parseError.errorCode <> 0 Then
    MsgBox ("XML connection error detected.")
End If

Set Root = objXMLDoc.documentElement 
Set NodeList = Root.getElementsByTagName("Vendor") 

Dim AddVendorDetails, InputFile, FSO, oFile, strTempSourceIDs 
InputFile = "C:\users\jdarling\20161109\01\temp-4.txt" 
Set FSO = CreateObject("Scripting.FileSystemObject") 
Set oFile = FSO.OpenTextFile(InputFile) 
strTempSourceIDs = oFile.ReadAll 
oFile.Close 

Dim splitCounter
Dim splitCatcher
Dim IDtest

SplitCatcher = Split(strTempSourceIDs, vbCrLf)
For splitCounter = 0 To UBound(SplitCatcher)
    IDtest = SplitCatcher(splitCounter)

    For Each i In NodeList 

        'Debug.WriteLine "Comparing: " & Trim(IDtest)& "  TO   " & Trim(i.firstChild.nodeTypedValue)

        If Trim(IDtest) =  Trim(i.firstChild.nodeTypedValue) Then

            Debug.WriteLine "FOUND! " & IDTest

            Dim IDnode
            Dim Sourcenode
            Dim Destnode
            Set SourceID = i.getElementsByTagName("SourceID")(0)
            Set SourceName = i.getElementsByTagName("SourceName")(0)
            Set DestName = i.getElementsByTagName("DestName")(0)

            If SourceID.nodeValue <> "" Then
                IDnode = SourceID.nodeValue
            Else
                IDnode = "ERROR:Undefined ID"
            End If
            If SourceName.nodeValue <> "" Then
                Sourcenode = SourceName.nodeValue
            Else
                Sourcenode = "(No Source Name in config files)"
            End If
            If DestName.nodeValue <> "" Then
                Destnode = DestName.nodeValue
            Else
                Destnode = "(No Dest Name in config files)"
            End If

            AddVendorDetails = IDnode & vbCrLf 
            AddVendorDetails = AddVendorDetails & "SourceName: " & Sourcenode & vbCrLf 
            AddVendorDetails = AddVendorDetails & "DestName: " & Destnode & vbCrLf 
            If SourceID.nodeValue <> "" Then
                MsgBox (AddVendorDetails)
            End If    
            strTempSourceIDs = Replace(strTempSourceIDs, IDnode, AddVendorDetails)
        End If
    Next
Next

strTempSourceIDs = "######################## " & WeekdayName(Weekday(Date)) & Date & " ########################" & vbCrLf & strTempSourceIDs
MsgBox (strTempSourceIDs)