使用VBScripting将节点添加到xml文件

时间:2014-09-01 11:23:55

标签: xml vbscript xmldom

在以下XML文件中,使用vbscript我想要添加节点

<?xml version="1.0" encoding="UTF-8"?>
<Project>
   <Variable name="Variable_1" />
   <Study>
      <Agent>
         <Variable name="Variable_1" baseline="0" distribution="" flags="2" max="1" min="0" />
      </Agent>
   </Study>
</Project>

运行脚本后,xml文件将如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<Project>
   <Variable name="Variable_1" />
   <Variable name="Variable_2" />
   <Variable name="Variable_3" />
   <Study>
      <Agent>
         <Variable name="Variable_1" baseline="0" distribution="" flags="2" max="1" min="0" />
         <Variable name="Variable_2" baseline="4" distribution="" flags="2" max="3" min="1" />
      </Agent>
   </Study>
</Project>

我尝试了以下代码,但没有结果

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("us2.xml")
Dim objCurrNode, objNewNode, objNewText 
Set objRoot = xmlDoc.documentElement
Set objNewNode = XMLDoc.createElement("VarIable name")
Set objNewText = XMLDoc.createTextNode("Variable_2")
objNewNode.appendChild(objNewText)
xmlDoc.Save "Audits.xml"  

请在此提出建议。 感谢。

2 个答案:

答案 0 :(得分:2)

这是一个示例,演示了您尝试的代码中未正确覆盖的某些技术,即(空检查已跳过):

  1. 为元素创建属性

  2. 将元素添加到父节点

  3. 在某个现有元素之后添加元素

  4. 使用Xpath和SelectSingleNode()方法

  5. 选择特定元素
    Set xmlDoc = CreateObject("Microsoft.XMLDOM")
    xmlDoc.Async = "False"
    xmlDoc.Load("us2.xml")
    
    Dim variable1, variable2, agent, variable2Agent
    'select <Variable> node having name attribute equals Variable_1'
    Set variable1 = xmlDoc.SelectSingleNode("/Project/Variable[@name='Variable_1']")
    'create new <Variable>'
    Set variable2 = xmlDoc.CreateElement("Variable");
    'add name="Variable_2" attribute'
    variable2.SetAttribute("name", "Variable_2");
    'add new <Variable> after variable1 node'
    variable1.ParentNode.InsertAfter(variable2, variable1);
    
    'select <Agent> node'
    Set agent = xmlDoc.SelectSingleNode("/Project/Study/Agent")
    'create new <Variable> and set all required attributes'
    Set variable2Agent = xmlDoc.CreateElement("Variable")
    variable2Agent.SetAttribute("name", "Variable_2")
    variable2Agent.SetAttribute("baseline", "4")
    ....
    'add new <Variable> after the last child of <Agent> node'
    agent.AppendChild(variable2Agent)
    
    xmlDoc.Save "Audits.xml" 
    

答案 1 :(得分:0)

你不能createElement s属性和属性值不是文本孩子。使用XPath查找模型节点,克隆并修改它们,然后将它们附加到正确的父节点。为了帮助您入门:

Option Explicit

Dim oXDoc  : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load "..\data\25604761.xml"

If 0 = oXDoc.ParseError Then
   WScript.Echo oXDoc.documentElement.xml
   Dim sXPath : sXPath     = "/Project/Variable"
   Dim ndlFnd : Set ndlFnd = oXDoc.selectNodes(sXPath)
   If 1 = ndlFnd.length Then
      Dim ndNew : Set ndNew = ndlFnd(0).cloneNode(True)
      ndNew.getAttributeNode("name").value = "Variable_2"
      ndlFnd(0).parentNode.appendChild ndNew
      WScript.Echo "----------------", vbCrLf & oXDoc.documentElement.xml
   Else
      WScript.Echo sXPath, "- not exactly one node found"
   End If
Else
   WScript.Echo oXDoc.ParseError.Reason
End If

输出:

cscript 25604761.vbs
<Project>
        <Variable name="Variable_1"/>
        <Study>
                <Agent>
                        <Variable name="Variable_1" baseline="0" distribution="" flags="2"
                </Agent>
        </Study>
</Project>
----------------
<Project>
        <Variable name="Variable_1"/>
        <Study>
                <Agent>
                        <Variable name="Variable_1" baseline="0" distribution="" flags="2"
                </Agent>
        </Study>
        <Variable name="Variable_2"/>
</Project>