在vbscript中处理xml,导致问题的新元素

时间:2016-07-21 19:37:12

标签: xml vbscript

我猜这很简单,但我并不精通处理xml数据。

我有一些vbscript代码以这种格式处理来自第三方的xml数据。

<data>
    <thing1>some thing</thing1>
    <thing2>some other thing</thing2>
    <parameter>
        <parameterName>customThing1</paramterName>
        <parameterValue>this is the data i want</parameterValue>
    </parameter>
</data>

以前,所有数据都来自thing1或thing2。现在,我们必须为数据添加自定义字段,第三方将在此参数中发送该字段&#39;格式。

我得到的旧数据如下:(objXmlRequest是一个MSXML2.DomDocument对象)

thing1 = objXmlRequest.documentElement.selectSingleNode("thing1").firstChild.nodeValue

现在我需要获取customThing1的值,但我不想只是拉出参数值&#39;因为如果我们将来添加另一个自定义字段将是一个问题。所以我需要确保我在参数名= customThing1的地方获得了paramterValue。我该怎么做?

1 个答案:

答案 0 :(得分:1)

基于this:使用customThing1 parameterName查找参数并访问其parameterValue:

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

If 0 = objMSXML.parseError Then
   Dim sXPath : sXPath  = "/data/parameter[parameterName=""customThing1""]/parameterValue"
   Dim ndX    : Set ndX = objMSXML.selectSingleNode(sXPath)
   If ndX Is Nothing Then
      WScript.Echo sXPath, "failed"
   Else
      WScript.Echo ndX.tagName, ndX.text
   End If
Else
   WScript.Echo objMSXML.parseError.reason
End If

输出:

cscript 38512955.vbs
parameterValue this is the data i want