开发者

Replace quotes in xml string

I have a problem with double quotes in classic ASP.

I want to replace double quotes " in a string. The string contains xml and I don't want to replace double quotes (for attributes) inside tags.

So if I wanted to replace double quotes with single quotes, I'd want my string to go from this:

<MyDinosaurDocument DocType="Brachysaurus">"Hello" said the little dinosaur</MyDinosaurDocument>

to this:

<MyDinosaurDocument Do开发者_运维技巧cType="Brachysaurus">'Hello' said the little dinosaur</MyDinosaurDocument>

I've tried using regular expressions and would like to fix this problem with them -- but I'm sadly out of my depth.

All and any help is greatly appreciated.


I wouldn't use Regex to solve this problem. Here is a simple chunk of code that would do it:-

Dim dom : Set dom = CreateObject("MSXML2.DOMDocument.3.0")

dom.LoadXml myXMLString

Dim node
For Each node in dom.SelectNodes("//*/text()")
    node.nodeValue = Replace(node.nodeValue, """", "'")
Next

myXMLString = dom.xml

Of course you probably at some point need to load the XML into a DOM anyway so once that is done there is no need to read the string back out.


Use &quot; to escape quotes in XML.


As always when dealing with non-regular data, the answer is not to use a regular expression. Really, don’t. XML and HTML should always be parsed by appropriate parsers and furthermore ASP gives you the means to do this easily. Using regular expressions here is a serious security liability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜