我有以下XML
<list name="Model">
<list name="Info">
<item name="ViewName">Page</item>
<item name="DBField">Number</item>
<item name="Type">int</item>
</list>
<list name="Info">
<item name="ViewName">Page</item>
<item name="DBField">Text</item>
<item name="Type">String</item>
</list>
<list name="Codes">
</list>
</list>
我想将新子<list name="Info">
添加到<list name="Model">
,
必须在最后<list name="Info">
之后但在<list name="Codes">
部分之前插入儿童。
我选择了最后一个信息节点,但无法弄清楚如何在代码节点之前插入节点。
答案 0 :(得分:1)
使用.insertBefore,如下所示:
Option Explicit
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument")
oXML.async = False
oXML.Load "..\data\32180204.xml"
If 0 = oXML.ParseError Then
Dim sXPath : sXPath = "/list/list[@name='Codes']"
Dim ndCodes : Set ndCodes = oXML.selectSingleNode(sXPath)
If ndCodes Is Nothing Then
WScript.Echo sXPath, "not found"
Else
Dim ndNew : Set ndNew = oXML.createElement("list")
ndCodes.parentNode.insertBefore ndNew, ndCodes
WScript.Echo oXML.xml
End If
Else
WScript.Echo oXML.ParseError.Reason
End If
输出:
cscript 32180204.vbs
<list name="Model">
<list name="Info">
<item name="ViewName">Page</item>
<item name="DBField">Number</item>
<item name="Type">int</item>
</list>
<list name="Info">
<item name="ViewName">Page</item>
<item name="DBField">Text</item>
<item name="Type">String</item>
</list>
<list/><list name="Codes">
</list>
</list>