将子条目添加到 vbscipt 中 xml 文件中的特定节点 [英] Add child entry to a specific node in xml file in vbscipt

查看:28
本文介绍了将子条目添加到 vbscipt 中 xml 文件中的特定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含此条目的 xml 文件 DataConfiguration.xml

I have an xml file DataConfiguration.xml with this entry

<DataSource>
 <localdata>
    <add context="Localization">
       <parameter name="timeout" type="int" defaultvalue="60"/>
       <parameter name="address" type="string" defaultvalue="192.168.9.45" />
       <parameter name="port" type="int" defaultvalue="6789"/>
    </add>
</localdata>
</DataSource>

我需要向localdata"添加另一个条目,所以它是

I need to add another entry to "localdata" so it would be

 <DataSource>
     <localdata>
        <add context="Localization">
           <parameter name="timeout" type="int" defaultvalue="60"/>
           <parameter name="address" type="string" defaultvalue="192.168.9.45" />
           <parameter name="port" type="int" defaultvalue="6789"/>
        </add>
       <add context="General">
           <parameter name="timeout" type="int" defaultvalue="60"/>
           <parameter name="address" type="string" defaultvalue="192.168.9.478" />
           <parameter name="port" type="int" defaultvalue="5674"/>
        </add>
    </localdata>
    </DataSource>

我如何在 vbscript 中添加这个?

How would I add this in vbscript?

我当前的代码

'created xml file object
Set xmlDoc = CreateObject("Msxml2.DOMDocument")

xmlDoc.async = False  
xmlDoc.preserveWhiteSpace= True 
xmlDoc.load("DataConfiguration.xml")

Dim entry

entry = "<add context=""General"">" & _
               <parameter name=""timeout"" type=""int"" defaultvalue=""60""/>" & _ 
               <parameter name=""address"" type=""string"" defaultvalue=""192.168.9.478"" />" & _
               <parameter name=""port"" type=""int"" defaultvalue=""5674""/>"& _
            </add>"

Set NewNode = xmlDoc.createElement(entry)
Set ElemList = xmlDoc.getElementsByTagName("localdata")
ElemList.appendChild(NewNode)

但这会导致错误

此名称不能包含 <字符" at " Set NewNode = xmlDoc.createElement(entry)

This name may not contain < character" at " Set NewNode = xmlDoc.createElement(entry)

ElemList.appendChild(NewNode) 也不起作用.

Also the ElemList.appendChild(NewNode) does not work.

推荐答案

XmlDocument.CreateElement 接受三个参数:节点类型、节点名称和命名空间.在您的示例中,由于您的子元素名为add",它是一个元素(type==1),并且它是全局 xml 命名空间的一部分,您可以调用 xmlDoc.CreateElement(1, "add","") .

XmlDocument.CreateElement accepts three params: a node type, a node name, and a namespace. In your example, since your child element is named "add", it's an element (type==1), and it is part of the global xml namespace, you would call xmlDoc.CreateElement(1, "add", "") .

这会给你一个空元素.要插入所需的数据(Context="General" 属性和所有子元素),您需要连续调用 DOM 操作方法,以添加每个子元素、每个属性等等.挺费劲的.

That gives you an empty element. To insert the data you want (the Context="General" attribute, and all the child elements), you'd then need to make successive calls to the DOM manipulation methods, to add in each child element, each attribute, and so on. Pretty laborious.

但是您已经将 xml 片段作为字符串.因此,您可以做的是创建第二个 XmlDocument 并告诉它从字符串中获取其内容,而不是使用 DOM 方法创建元素.然后从第二个文档中获取 documentElement.然后在第一个文档中的适当节点上调用 appendChild,传递第二个文档中的 documentElement.

But you already have the xml fragment as a string. So instead of creating the element using DOM methods, what you can do is create a 2nd XmlDocument and tell it to get its content from the string. Then grab the documentElement from that 2nd doc. Then call appendChild on the appropriate node in first doc, passing the documentElement from the 2nd doc.

像这样:

Function GetElementFromXmlString(xmlString)
    Dim doc
    set doc = CreateObject("Msxml2.DOMDocument.6.0")
    doc.async = False
    doc.preserveWhiteSpace= False
    doc.loadXML(xmlString)
    Set GetElementFromXmlString = doc.documentElement
End Function

Sub Main()
    Set doc1 = CreateObject("Msxml2.DOMDocument.6.0")
    doc1.async = False
    doc1.preserveWhiteSpace= False ' True
    doc1.load("DataConfiguration.xml")

    ' generate an Element from an XML string
    Dim xmlString
    xmlString = "<add context=""General"">" & _
                  " <parameter name=""timeout"" type=""int"" defaultvalue=""60""/>" & _
                  " <parameter name=""address"" type=""string"" defaultvalue=""192.168.9.478"" />" & _
                  " <parameter name=""port"" type=""int"" defaultvalue=""5674""/>"& _
              "</add>"
    Dim newElt
    Set newElt = GetElementFromXmlString(xmlString)

    ' get the first child node of type=Element under the document root element in
    ' doc1.  This is not the same as  doc1.documentElement.firstChild.  There can
    ' be text nodes, etc.
    Dim node1
    Set node1 = doc1.documentElement.selectSingleNode("./*[position()=1]")

    ' append the element to the node
    node1.appendChild(newElt)

    WScript.echo (PrettyPrintXml (doc1))
End Sub

Main()

...其中 PrettyPrintXml 函数的定义如下:

...where the PrettyPrintXml function is defined like this:

Function PrettyPrintXml(xmldoc)
    Dim reader
    set reader = CreateObject("Msxml2.SAXXMLReader.6.0")
    Dim writer
    set writer = CreateObject("Msxml2.MXXMLWriter.6.0")
    writer.indent = True
    writer.omitXMLDeclaration = True
    reader.contentHandler = writer
    reader.putProperty "http://xml.org/sax/properties/lexical-handler", writer
    reader.parse(xmldoc)
    PrettyPrintXml = writer.output
End Function

对我来说,这个输出是:

The output of this, for me, is:

<DataSource>
  <localdata>
    <add context="Localization">
      <parameter name="timeout" type="int" defaultvalue="60"/>
      <parameter name="address" type="string" defaultvalue="192.168.9.45"/>
      <parameter name="port" type="int" defaultvalue="6789"/>
    </add>
    <add context="General">
      <parameter name="timeout" type="int" defaultvalue="60"/>
      <parameter name="address" type="string" defaultvalue="192.168.9.478"/>
      <parameter name="port" type="int" defaultvalue="5674"/>
    </add>
  </localdata>
</DataSource>

这篇关于将子条目添加到 vbscipt 中 xml 文件中的特定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆