VB脚本修改XML文件节点

VB脚本修改XML文件节点,xml,xpath,vbscript,Xml,Xpath,Vbscript,这是一个示例XML文档内容: <row> <RaceNumber>131</RaceNumber> <title1>Cedar County Board of Supervisors</title1> <PrecintPercent>100</PrecintPercent> <Winner>5149</Winner> <WinningVotes

这是一个示例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>

131
雪松县监事会
100
5149
6213
R
杰夫·考夫曼
1.
28
5148
4628
R
韦恩·迪尔伯格
21
1.
雪松县监事会
100
5376
4407
R
琼贝尔
1.
100
5151
4141
D
戴维申克
0
雪松县监事会
100
5150
3167
D
罗伯特普鲁斯
0
我想知道我是否可以使用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>

雪松县监事会
100
5149
6213
R
杰夫·考夫曼
1.
28
5148
4628
R
韦恩·迪尔伯格
21
1.
雪松县监事会
100
5376
4407
R
琼贝尔
1.
100
5151
4141
D
戴维申克
0
基本上,我想在标题为
的任何标记之前写
,在标题为
的任何标记之后写一个结束标记

有人认为这是可能的吗?如果是这样,我想让它在整个文档中编写
标记,其中会遇到


任何输入都将是伟大的!谢谢大家!

这应该符合你的要求。它将在
之前和
之后粘贴

Set objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objReadXmlFile=objFSO.OpenTextFile(“file.xml”)
设置objNewXmlFile=objFSO.CreateTextFile(“file.new.xml”,True)
boolSkipCloseElection=False
strPrevLine=“”
不执行objReadXmlFile.AtEndOfStream时执行
strLine=objReadXmlFile.ReadLine()
如果InStr(1,strLine,“”),则
如果InStr(1,strPrevLine,“”)=0,则
objNewXmlFile.Write“”&vbCrLf
其他的
boolSkipCloseElection=True
如果结束
objNewXmlFile.Write strLine和vbCrLf
ElseIf InStr(1,strLine,“”)然后
objNewXmlFile.Write strLine和vbCrLf
如果boolSkipCloseElection=True,则
boolSkipCloseElection=False
其他的
objNewXmlFile.Write“”&vbCrLf
如果结束
其他的
objNewXmlFile.Write strLine和vbCrLf
如果结束
strPrevLine=strLine
环
objReadXmlFile.Close
objNewXmlFile.Close
编辑:如果这些标记已经存在,则不会插入它们。

这(就像任何涉及XML的内容一样)是为XML设计的工具的工作

XSLT就是这样一种工具。抵制对XML进行字符串替换的诱惑

<!-- elections.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" encoding="UTF-8" />

  <xsl:key name="kElection" match="node()[not(self::title1)]" 
    use="generate-id(preceding-sibling::title1[1])"
  />

  <xsl:template match="row[ELECTION]">
    <xsl:copy-of select="." />
  </xsl:template>

  <xsl:template match="row[not(ELECTION)]">
    <xsl:copy>
      <xsl:apply-templates select="title1" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="title1">
    <ELECTION>
      <xsl:copy-of select=". | key('kElection', generate-id(.))" />
    </ELECTION>
  </xsl:template>
</xsl:stylesheet>
但存在更简单的方法。您可以直接从命令行执行以下操作:

msxsl your_input.xml elections.xsl -o your_output.xml

以下是XSLT程序的工作原理:

  • 它只保留已经包含
    元素的行(即,它只是按原样将它们复制到输出中)
  • 它使用XSL键将文件中的所有节点按其前面的第一个
    分组。这样,可以一起检索属于某个标题的所有元素
  • 它查看所有
    元素,并制作它们及其关联组的副本,分别将它们包装在
    元素中

这种方法比简单的字符串替换更安全、更通用。

我知道这很糟糕,但如果XML总是那么简单,那么肯定可以用正则表达式来替换
。XML输出总是这样。但是,有些输出中有21个
。我会看看能不能研究一下。非常感谢。这正是你所说的。因此,我必须非常感谢你!我现在需要弄清楚如何正确解析DangXML。哈我现在将看看这是否将是一个切实可行的方法来让它发挥作用。再次感谢!!!!嘿,如果我想让它忽略任何已经包含和的记录呢?我有一些已经有了这些标签,这导致了多个嵌套对象。我不确定我的解析器是否能够识别这些嵌套的,但我会在明天或今天到达办公室时发现。哎呀!更新了我的答案以反映这一点。希望能成功。您正在使用文本工具编辑XML文件。这是一种被驳斥了一千次的愚蠢做法-1@QuickNull这是我最接近获得所需输出的一次!唯一的问题是,在2452行XML中,有6个
标记没有写入
标记。我确实查看了输出XML文件,并手动将
标记添加到该文件中,并且能够解析XML。这是我发现的唯一问题。非常感谢你!谢谢你的澄清。也许,与其说答案愚蠢和幼稚来侮辱某人,不如用它来教育某人。这似乎是这个网站的重点。虽然使用原始问题中提供的数据测试代码并不能输出他想要的内容。似乎如果标签已经存在,它会创建标签。但是在他的输入中没有标签存在,他也没有说会是这样。当然——以此类推——修改XSLT程序来解释这一点相当容易,修改基于字符串替换的解决方案则相当困难。当然,我做得相当严厉,但这不是针对个人的。我经常看到它,它真的磨我的齿轮(决不是我有一个发痒的手指,否则)。将XML/HTML视为文本并在其上进行字符串替换是一种“假朋友”解决方案,它看似简单且足够好,但却会造成各种破坏,从字符编码故障到输入混乱,再到XSS/注入漏洞,再到无法维护基于正则表达式的漏洞,所以当我看到有人推荐他们时,我有点紧张。另外,“天真”也不是一种侮辱。“天真”一词
Dim xml, xsl, xmlOut

Set xml = CreateObject("MSXML2.DOMDocument")
Set xsl = CreateObject("MSXML2.DOMDocument")

xml.async = False
xsl.async = False

xml.load "your_input.xml"
xsl.load "elections.xsl"

xml.transformNodeToObject xsl, xml
xml.save "your_output.xml"
msxsl your_input.xml elections.xsl -o your_output.xml