最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Escape special characters in XML documents - Stack Overflow

programmeradmin2浏览0评论

I have a set of button tags on a webpage and I want to get one particular button tag whose innerText is "Save". (It has no id to it.) So I have this code

var tags = document.getElementsByTagName("button");
for (var i = 0; i < tags.length; i++) {
    if (tags[i].innerText === 'Save') {
        tags[i].click();
        break;
    }
}

which works perfectly when I try it in chrome console. But I can't include this in my jelly file(which is an xml markup that will be processed into html; something like a jsp.)

The problem is with the "<" operator in the for loop which is causing this

SAXParserException: "The content of elements must consist of well-formed character data or markup."

And I learnt not to use for..in loops with arrays. What can I do? Please suggest me some workaround.

I have a set of button tags on a webpage and I want to get one particular button tag whose innerText is "Save". (It has no id to it.) So I have this code

var tags = document.getElementsByTagName("button");
for (var i = 0; i < tags.length; i++) {
    if (tags[i].innerText === 'Save') {
        tags[i].click();
        break;
    }
}

which works perfectly when I try it in chrome console. But I can't include this in my jelly file(which is an xml markup that will be processed into html; something like a jsp.)

The problem is with the "<" operator in the for loop which is causing this

SAXParserException: "The content of elements must consist of well-formed character data or markup."

And I learnt not to use for..in loops with arrays. What can I do? Please suggest me some workaround.

Share Improve this question edited Apr 19, 2012 at 6:23 Dagg Nabbit 76.8k19 gold badges114 silver badges142 bronze badges asked Apr 19, 2012 at 6:03 VigneshwaranVigneshwaran 3,2756 gold badges25 silver badges36 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You are solving the wrong problem. Your problem is "Including a < character in XML breaks the XML". You need to find out how to include such a character correctly, not avoid having one ever appear in your data. There is no need to avoid a standard for loop.

Either wrap that section with CDATA markers (which stop XML special characters (except the end of CDATA sequence) being special) or represent the < with &lt; in the XML.

<![CDATA[
for (var i = 0; i < j; i++) {
    etc(i);
}
]]>

or

for (var i = 0; i &lt; j; i++) {
    etc(i);
}

You can iterate with the new Array.forEach method, but it's available from JavaScript 1.6 only:

var tags = Array.prototype.slice.call(document.getElementsByTagName("button"));
tags.forEach(function (tag) {
  // ...
});

But the real solution would be to put your code into <![CDATA[]]>:

<code>
<![CDATA[
var tags = document.getElementsByTagName("button");
for (var i = 0; i < tags.length; i++) {
  // ...
}
]]>
</code>
var limit = tags.length;

//loop until 0 (which is false)
while(limit) {
    if (tags[tags.length-limit].innerText === 'Save') {
        tags[tags.length-limit].click();
        break;
    }
    limit--;
}

Put it inside <[CDATA[ ... ]]>

Editing: Wrong syntax...

发布评论

评论列表(0)

  1. 暂无评论