Reputation: 239
I have the following XML that need to parsed using ASP VBscript,
<ERROR>
<ITEM>
<CODE>X11</CODE>
<TEXT>E112</TEXT>
</ITEM>
<ITEM>
<CODE>X14</CODE>
<TEXT>E888</TEXT>
</ITEM>
<OTHER-INFO>
<XID>I989</XID>
<OTHER-INFO>
<ERROR>
My requirement is to replace TEXT value inside ITEM with its corresponding detailed information that I will get it from getInfo(code) function. How to replace TEXT value using classic asp and vbscript.
output required,
<ITEM>
<CODE>X11</CODE>
<TEXT>Detailed Explanation about this error</CODE>
</ITEM>
I have written the following code to iterate through each ITEM. objMSXML contains the above tags and i need the results back in the same object.
If objMSXML.getElementsByTagName("ERROR").length <> 0 Then
For Each ObjNode In objMSXML.documentElement.selectNodes("ITEM")
strTrasMsg = getInfo(objECGO.xVal("CODE",ObjNode,"STR"))
Next
End If
Upvotes: 1
Views: 1960
Reputation: 38745
Short answer:
For Each ObjNode In objMSXML.documentElement.selectNodes("ITEM")
strTrasMsg = "whatever-" & ObjNode.firstChild.text ' getInfo(objECGO.xVal("CODE",ObjNode,"STR"))
WScript.Echo strTrasMsg, ObjNode.childNodes(1).text
ObjNode.childNodes(1).text = strTrasMsg
Next
that is: use the DOM tree and access child nodes by index.
Long answer:
If you (all) present your "edit my xml"-problems according to this skeleton:
Dim sXml : sXml = Join(Array(_
"<ERROR>" _
, " <ITEM>" _
, " <CODE>X11</CODE>" _
, " <TEXT>E112</TEXT>" _
, " </ITEM>" _
, " <ITEM>" _
, " <CODE>X14</CODE>" _
, " <TEXT>E888</TEXT>" _
, " </ITEM>" _
, " <OTHER-INFO>" _
, " <XID>I989</XID>" _
, " </OTHER-INFO>" _
, "</ERROR>" _
))
Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
objMSXML.setProperty "SelectionLanguage", "XPath"
objMSXML.async = False
objMSXML.loadXml sXml
If 0 = objMSXML.parseError Then
If objMSXML.getElementsByTagName("ERROR").length <> 0 Then
Dim ObjNode, strTrasMsg
For Each ObjNode In objMSXML.documentElement.selectNodes("ITEM")
strTrasMsg = "whatever-" & ObjNode.firstChild.text '
---- problem -----
Next
WScript.Echo objMSXML.xml
End If
Else
WScript.Echo objMSXML.parseError.reason
End If
output:
whatever-X11 E112
whatever-X14 E888
<ERROR>
<ITEM>
<CODE>X11</CODE>
<TEXT>whatever-X11</TEXT>
</ITEM>
<ITEM>
<CODE>X14</CODE>
<TEXT>whatever-X14</TEXT>
</ITEM>
<OTHER-INFO>
<XID>I989</XID>
</OTHER-INFO>
</ERROR>
trivial problems of well-formness and validity could be solved before posting and solutions would flock in much faster.
Upvotes: 0
Reputation: 189457
Try the following (assumes a TEXT element does exist for each ITEM).
For Each oItem In objMSXML.documentElement.selectNodes("ITEM")
oItem.selectSingleNode("TEXT").text = getInfo(objECGO.xVal("CODE", oItem ,"STR"))
Next
Upvotes: 0
Reputation: 8062
Can you try using xpath to select node and replace the text
something like, I did not tried this but it should work, as xpath is supported
Set Node = xmlDoc.selectsinglenode("//ERROR/ITEM[../CODE/text()="item code vlaue"/text)
Node.text = "NEW VALUE"
xmldoc.save
Or another approach would be recreating the response by writing tags, as you already populated the code and description like.
var myxml = "<ITEM><CODE>"your value"</CODE><TEXT>"your value"</CODE></ITEM>
something than save back this xml.
Upvotes: 1