将子条目添加到 vbscipt 中 xml 文件中的特定节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4328907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add child entry to a specific node in xml file in vbscipt
提问by fireBand
I have an xml file DataConfiguration.xml with this entry
我有一个包含此条目的 xml 文件 DataConfiguration.xml
<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>
I need to add another entry to "localdata" so it would be
我需要在“localdata”中添加另一个条目,这样就可以了
<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>
How would I add this in vbscript?
我将如何在 vbscript 中添加它?
My Current code
我的当前代码
'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)
But this give the error
但这给出了错误
This name may not contain < character" at " Set NewNode = xmlDoc.createElement(entry)
此名称不能包含 < 字符" 在 " Set NewNode = xmlDoc.createElement(entry)
Also the ElemList.appendChild(NewNode) does not work.
此外, ElemList.appendChild(NewNode) 不起作用。
回答by Cheeso
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", "").
XmlDocument.CreateElement 接受三个参数:节点类型、节点名称和命名空间。在您的示例中,由于您的子元素名为“add”,因此它是一个元素(type==1),并且它是全局 xml 命名空间的一部分,您将调用xmlDoc.CreateElement(1, "add", "").
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.
这给了你一个空元素。要插入所需的数据(Context="General"属性和所有子元素),您需要连续调用 DOM 操作方法,添加每个子元素、每个属性等等。挺费劲的。
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.
但是您已经将 xml 片段作为字符串。因此,您可以做的是创建第二个 XmlDocument 并告诉它从字符串中获取其内容,而不是使用 DOM 方法创建元素。然后从第二个文档中获取 documentElement。然后在第一个文档中的适当节点上调用 appendChild,传递第二个文档中的 documentElement。
something like this:
像这样:
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()
...where the PrettyPrintXml function is defined like this:
...其中 PrettyPrintXml 函数的定义如下:
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>

