[英]Unexpected token ILLEGAL when load xml object?
从数据库和元帅到String的兴起
String xml = XMLUtils.marshallToString(list);
sre.getServletRequest().setAttribute("LIST", xml);
和代码JS
var regObject = '${requestScope.LIST}';
.....在浏览器中打开时。 查看源我有错误意外令牌非法。
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>';
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();
有人知道如何解决此问题吗?
JavaScript的'
和"
字符串文字不能在其中包含转义的换行符,因此会出现错误。(ES2015的反引号模板字符串可以。)
在输出XML时,您需要确保JavaScript字符串文字中的任何特殊字符,例如'
(因为您使用单引号),换行符和反斜杠,都在转义符前加上反斜杠他们。
例如,您希望您的输出看起来像这样:
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>';
或者,当然,更换新行\\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.