vbscript使用特殊字符创建转换xml [英] vbscript create-convert xml with special characters

查看:30
本文介绍了vbscript使用特殊字符创建转换xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 .vbs 文件中的 xml 文件,其节点值如下所示,

I'm creating a xml file in a .vbs file with node values like the following,

  <car>David's</car>
  <company>Mannar & Co.</company>

在解析这个 xml 时,我发现了 & 等问题

While parsing this xml, I find issues with &, etc.

我想将所有可能的 xml 特殊字符与编码字符(使用函数 或其他东西)进行转换,以便在解析时获得原始内容.

I want to convert all possible xml special characters with encoded characters(with a function or something) so that while parsing I get the original content.

谢谢.

推荐答案

根据 OP 这里的评论,我自己制作的版本,找不到可靠的,我认为它涵盖了所有可能的 ascii 字符

Based on the comment of OP here i version i made myself, couldn't find a reliable one, i think it covers all possible ascii characters

Function HTML_Encode(byVal string)
  Dim tmp, i 
  tmp = string
  For i = 160 to 255
    tmp = Replace(tmp, chr(i), "&#" & i & ";")
  Next
  tmp = Replace(tmp, chr(34), "&quot;")
  tmp = Replace(tmp, chr(39), "&apos;")
  tmp = Replace(tmp, chr(60), "&lt;")
  tmp = Replace(tmp, chr(62), "&gt;")
  tmp = Replace(tmp, chr(38), "&amp;")
  tmp = Replace(tmp, chr(32), "&nbsp;")
  HTML_Encode = tmp
End Function

Function HTML_Decode(byVal encodedstring)
  Dim tmp, i
  tmp = encodedstring
  tmp = Replace(tmp, "&quot;", chr(34) )
  tmp = Replace(tmp, "&apos;", chr(39))
  tmp = Replace(tmp, "&lt;"  , chr(60) )
  tmp = Replace(tmp, "&gt;"  , chr(62) )
  tmp = Replace(tmp, "&amp;" , chr(38) )
  tmp = Replace(tmp, "&nbsp;", chr(32) )
  For i = 160 to 255
    tmp = Replace(tmp, "&#" & i & ";", chr(i))
  Next
  HTML_Decode = tmp
End Function

str = "This !@#± is a & test!"
wscript.echo HTML_Encode(str) '=> This&nbsp;!@#&amp;#177;&nbsp;is&nbsp;a&nbsp;&amp;&nbsp;test!
wscript.echo HTML_Decode(HTML_Encode(str)) '=> This !@#± is a & test!

这篇关于vbscript使用特殊字符创建转换xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆