Java XML Unmarshalling 在使用 JAXB 的与号 (&) 上失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2999132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java XML Unmarshalling fails on ampersand (&) using JAXB
提问by ryanprayogo
I have the following XML:
我有以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<details>
...
<address1>Test&Address</address1>
...
</details>
When I try to unmarshal it using JAXB, it throws the following exception:
当我尝试使用 JAXB 对其进行解组时,它会引发以下异常:
Caused by: org.xml.sax.SAXParseException: The reference to entity "Address" must end with the ';' delimiter.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)
But when I changed the &in the XML to ', it works. Looks like the problem is only with ampersand &and I cannot understand why.
但是当我&将 XML 中的更改为 时',它起作用了。看起来问题只是与符号有关&,我不明白为什么。
The code to unmarshal is:
解组的代码是:
JAXBContext context = JAXBContext.newInstance("some.package.name", this.getClass().getClassLoader());
Unmarshaller unmarshaller = context.createUnmarshaller();
obj = unmarshaller.unmarshal(new StringReader(xml));
Anyone have some insight?
任何人都有一些见解?
EDIT: I tried the solution suggested by @abhin4v below (ie, add a space after &), but it doesn't seem to work too. Here's the stacktrace:
编辑:我尝试了下面@abhin4v 建议的解决方案(即,在 之后添加一个空格&),但它似乎也不起作用。这是堆栈跟踪:
Caused by: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)
采纳答案by ryanprayogo
It turns out that the problem is because of the framework I'm using (Mentawai framework). The said XML comes from the POST body of an HTTP request.
事实证明,问题出在我使用的框架(Mentawai 框架)上。所述 XML 来自 HTTP 请求的 POST 正文。
Apparently, the framework converts the character entities in the XML body, therefore, &becomes &and the unmarshaller fails to unmarshal the XML.
显然,框架转换的字符实体在XML主体,因此,&变成&与解组未能解组XML。
回答by Abhinav Sarkar
Xerces converts &to &and then tries to resolve &Addresswhich fails because it does not end with ;. Put a space between Putting a space will not work as Xerces will now try to resolve &and Addressand it should work.&and throw the second error given in OP. You can wrap the test in a CDATA section and Xerces will not try to resolve the entities.
Xerces 转换&为&,然后尝试解决&Address哪个失败,因为它不以;. 在放置空格将不起作用,因为 Xerces 现在将尝试解决&和之间留一个空格Address,它应该可以工作。&并抛出 OP 中给出的第二个错误。您可以将测试包装在 CDATA 部分中,Xerces 不会尝试解析实体。
回答by Quotidian
I've run into this too. First pass I simply replaced the & to a token string (AMPERSAND_TOKEN), sent it through JAXB, then re-replaced the ampersand. Not ideal, but it was a quick fix.
我也遇到过这个。第一遍我简单地将 & 替换为令牌字符串 (AMPERSAND_TOKEN),通过 JAXB 发送它,然后重新替换与符号。不理想,但它是一个快速修复。
Second pass I made a lot of significant changes, so I'm not sure what exactly solved the problem. I suspect that providing JAXB access to the html dtds made it much happier, but that's only a guess and could be specific to my project.
第二遍我做了很多重大改变,所以我不确定到底是什么解决了这个问题。我怀疑提供对 html dtds 的 JAXB 访问使它更快乐,但这只是一个猜测,可能特定于我的项目。
HTH
HTH

