encodeURIComponent or Escape in VB6

2019-09-01 06:57发布

问题:

Is there a encodeURIComponent or Uri.EscapeDataString in VBScript that can be used in an classic ASP page?

I need this function to generate jQuery parseable XML.

Server.HTMLEncode does not do a complete job.

回答1:

You can just escape the characters yourself if you would like, according to this link, there are only five: What characters do I need to escape in XML documents?

Therefore, something like this should work:

//usage
EncodedXML = XMLEncode(MyDecodedXML)

//function
Function XMLEncode(str)
  XMLEncode = str
  If varType(XMLEncode) < 2 Exit Function
  XMLEncode = Replace(XMLEncode,Chr(34),"&quot;")
  XMLEncode = Replace(XMLEncode,"'","&apos;")
  XMLEncode = Replace(XMLEncode,"<","&lt;")
  XMLEncode = Replace(XMLEncode,">","&gt;")
  XMLEncode = Replace(XMLEncode,"&","&amp;")
End Function

Otherwise, this is generally done using the MSXML2.DOMDocument object in VBS. Documentation is available here. A simple example of its use is ...

sFilePath = "D:\someplace\somefile.xml"
sXPath = "/SomeName/Someproperty"

Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
oXMLDoc.SetProperty "SelectionLanguage", "XPath"
oXMLDoc.Async = False
oXMLDoc.Load sFilePath
If (oXMLDoc.parseError.errorCode <> 0) Then
   Set oErr = oXMLDoc.ParseError
   WScript.Echo "Could not load file " & sFilePath _
              & " , error: " & oErr.Reason
   WScript.Quit
End If

Set objNames = oXMLDoc.DocumentElement.SelectNodes(sXPath)
For Each obj in objNames
  with obj
    wsh.echo .nodeName,  .getAttribute("name")
  end with
Next