简体   繁体   English

加载xml对象时出现意外的令牌ILLEGAL?

[英]Unexpected token ILLEGAL when load xml object?

A seesion from database and marshal to String 从数据库和元帅到String的兴起

String xml = XMLUtils.marshallToString(list);
sre.getServletRequest().setAttribute("LIST", xml);

and code JS 和代码JS

var regObject = '${requestScope.LIST}';

..... when open in browser. .....在浏览器中打开时。 View source i have error Unexpected token ILLEGAL. 查看源我有错误意外令牌非法。

  var regObject = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>//Unexpected token ILLEGAL <items> <item> <id>ID</id> <productName>PrdName</productName> <productLink>LINK</productLink> <productImage>IMG</productImage> </item> </items>'; 
code marshalToString: 代码marshalToString:

 JAXBContext jaxb = JAXBContext.newInstance(List.class); Marshaller mar = jaxb.createMarshaller(); mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); StringWriter sw = new StringWriter(); mar.marshal(items, sw); return sw.toString(); 

Anyone know how to fix this problem? 有人知道如何解决此问题吗?

JavaScript's ' and " string literals can't have unescaped newlines in them, hence the error you're getting. (ES2015's backtick template strings can.) JavaScript的'"字符串文字不能在其中包含转义的换行符,因此会出现错误。(ES2015的反引号模板字符串可以。)

When outputting the XML, you'll want to ensure that any character that may be special inside a JavaScript string literal, such as ' (because you're using single quotes), newlines, and backslashes, are escaped with a backslash in front of them. 在输出XML时,您需要确保JavaScript字符串文字中的任何特殊字符,例如' (因为您使用单引号),换行符和反斜杠,都在转义符前加上反斜杠他们。

Eg, you want your output to look like this: 例如,您希望您的输出看起来像这样:

var regObject = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
<items>\
    <item>\
        <id>ID</id>\
        <productName>PrdName</productName>\
        <productLink>LINK</productLink>\
        <productImage>IMG</productImage>\
        <something>I\'m an example with an apostrophe</productImage>\
        <something>I\'m an example with a \\ (backslash)</productImage>\
    </item>\
</items>';

Or of course, replace newlines with \\n ( after escaping any existing backslashes): 或者,当然,更换新行\\n (逃避任何现有反斜线 ):

var regObject = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<items>\n<item>\n<id>ID</id>\n<productName>PrdName</productName>\n<productLink>LINK</productLink>\n<productImage>IMG</productImage>\n<something>I\'m an example with an apostrophe</productImage>\n<something>I\'m an example with a \\ (backslash)</productImage>\n</item>\n</items>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM