Mithilesh Indurkar
Mithilesh Indurkar

Reputation: 489

How to write special characters as they are to XML file

I am trying to create files based on the data provided in excel using vbscript/QTP. The problem I have is with negative testcases having special characters. I want to write special characters as node text but they are getting auto converted to escape characters.

Text to be written : address line one is<>"'&amp equal to 3 Actual Text after saving the file: address line one is&lt;&gt;&quot;&apos;&amp equal to 3

Is there any way to tell vbscript/XMLDOM not to convert these.

Code I am using is simple one.

Set  NodeList = uxtXMLDoc.documentElement.selectSingleNode(strNodePath)

If  Not(NodeList is Nothing) Then                                       
    NodeList.childNodes(0).nodeValue = "address line one is&lt;&gt;&quot;&apos;&amp equal to 3"
End If

Upvotes: 1

Views: 1630

Answers (2)

Mithilesh Indurkar
Mithilesh Indurkar

Reputation: 489

With the use of @Kira 's solution and solutions posted at the below link I was able to create my solution. The issue is that XML replaces linearly from start to end.

Hence first ampersand in ("&lt;") will be replaced as ("&amp;lt") instead of (<). 

You have to proactively replace the characters before XML replaces them.

strReplaceText = Replace(strtext, "&lt;", chr(60)) //Sample... Repeat for all the chars
NodeList.childNodes(0).nodeValue = strReplaceText 
objXML.Save(destination)

For more info, visit this thread. vbscript create-convert xml with special characters

Upvotes: 0

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Use the following line inside the 'If' condition:

NodeList.childNodes(0).nodeValue = "address line one is"&Chr(60)&Chr(62)&Chr(34)&chr(39)&"&amp equal to 3"

Here,

  chr(60) means <
  chr(62) means >
  chr(34) means "
  chr(39) means '

Here is the result: enter image description here

Upvotes: 2

Related Questions