encodeURIComponent or Escape in VB6

落花浮王杯 提交于 2019-12-12 02:58:38

问题


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


来源:https://stackoverflow.com/questions/25906657/encodeuricomponent-or-escape-in-vb6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!