这是一个示例XML文档内容:

<row>
    <RaceNumber>131</RaceNumber>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5149</Winner>
    <WinningVotes>6213</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner1>Jeff Kaufmann</Winner1>
    <WinnerSelected>1</WinnerSelected>
    <WinnerPercent>28</WinnerPercent>
    <Loser>5148</Loser>
    <LosingVotes>4628</LosingVotes>
    <LosingParty>R</LosingParty>
    <Loser2>Wayne Deerberg</Loser2>
    <LoserPercent>21</LoserPercent>
    <LoserSelected>1</LoserSelected>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5376</Winner>
    <WinningVotes>4407</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner>JonBell</Winner>
    <WinnerSelected>1</WinnerSelected>
    <PrecintPercent>100</PrecintPercent>
    <Loser>5151</Loser>
    <LosingVotes>4141</LosingVotes>
    <LosingParty>D</LosingParty>
    <Loser>DavidShinker</Loser>
    <LoserSelected>0</LoserSelected>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5150</Winner>
    <WinningVotes>3167</WinningVotes>
    <WinningParty>D</WinningParty>
    <Winner>RobertPruess</Winner>
    <WinnerSelected>0</WinnerSelected>
  </row>


我想知道是否可以使用VB脚本修改文件以使其看起来像这样吗?:

<row>
<ELECTION>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5149</Winner>
    <WinningVotes>6213</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner1>Jeff Kaufmann</Winner1>
    <WinnerSelected>1</WinnerSelected>
    <WinnerPercent>28</WinnerPercent>
    <Loser>5148</Loser>
    <LosingVotes>4628</LosingVotes>
    <LosingParty>R</LosingParty>
    <Loser2>Wayne Deerberg</Loser2>
    <LoserPercent>21</LoserPercent>
    <LoserSelected>1</LoserSelected>
</ELECTION>
<ELECTION>
    <title1>Cedar County Board of Supervisors</title1>
    <PrecintPercent>100</PrecintPercent>
    <Winner>5376</Winner>
    <WinningVotes>4407</WinningVotes>
    <WinningParty>R</WinningParty>
    <Winner>JonBell</Winner>
    <WinnerSelected>1</WinnerSelected>
    <PrecintPercent>100</PrecintPercent>
    <Loser>5151</Loser>
    <LosingVotes>4141</LosingVotes>
    <LosingParty>D</LosingParty>
    <Loser>DavidShinker</Loser>
    <LoserSelected>0</LoserSelected>
</ELECTION>


基本上,我想在标题为<ELECTION>的任何标签之前写<title1>,并在标题为</ELECTION>的任何标签之后写一个结束标签</LoserSelected>

有人认为这可能吗?如果是这样,我希望它在会遇到<ELECTION></ELECTION>的整个文档中写入<title1></LoserSelected>标记。

任何输入都会很棒!谢谢!

最佳答案

这应该可以满足您的要求。它将在<ELECTION>之前粘贴<title1>,在</ELECTION>之后粘贴</LoserSelected>

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objReadXmlFile = objFSO.OpenTextFile("file.xml")
Set objNewXmlFile = objFSO.CreateTextFile("file.new.xml", True)

boolSkipCloseElection = False
strPrevLine = ""

Do While Not objReadXmlFile.AtEndOfStream
    strLine = objReadXmlFile.ReadLine()

    If InStr(1, strLine, "<title1>") Then
        If InStr(1, strPrevLine, "<ELECTION>") = 0 Then
            objNewXmlFile.Write "<ELECTION>" & vbCrLf
        Else
            boolSkipCloseElection = True
        End If

        objNewXmlFile.Write strLine & vbCrLf
    ElseIf InStr(1, strLine, "</LoserSelected>") Then
        objNewXmlFile.Write strLine & vbCrLf
        If boolSkipCloseElection = True Then
            boolSkipCloseElection = False
        Else
            objNewXmlFile.Write "</ELECTION>" & vbCrLf
        End If
    Else
        objNewXmlFile.Write strLine & vbCrLf
    End If

    strPrevLine = strLine

Loop

objReadXmlFile.Close
objNewXmlFile.Close


编辑:如果它们已经存在,将不会插入这些标签。

10-07 20:35