[英]JAXB XML unmarshal with / in attribute value
I'm having an issue unmarshaling an XML file when some special characters as "/" are included inside one attribute's value like this one: 当像这样的一个属性值中包含一些特殊字符“ /”时,我在解组XML文件时遇到问题:
<field name = "test" value = "test&/"/>
I'm using the libraries woodstox-core (v5.0.3) and stax2-api (3.1.4) 我正在使用库woodstox-core(v5.0.3)和stax2-api(3.1.4)
The attribute value is defined in the XSD as a normalized String, that I think allows the character "/": 该属性值在XSD中定义为规范化的String,我认为允许使用字符“ /”:
<xs:element name="field" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="value" type="xs:normalizedString" use="required" />
</xs:complexType>
</xs:element>
But when making the unmarshal call, the exception is thrown: 但是,在进行unmarshal调用时,会引发异常:
XMLStreamReader xsr = null;
try {
// Create the XML stream reader
XMLInputFactory xif = XMLInputFactory.newFactory();
xsr = xif.createXMLStreamReader(inputStream, "UTF-8");
// Unmarshall the XML with JAXB, with XML schema validation enabled
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(this.xmlSchema);
Root rootIndex = (Root) unmarshaller.unmarshal(xsr);
[...]
}
And here the exception: 这里是例外:
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '/' (code 47) (expected a name start character)
at [row,col {unknown-source}]: [17,74]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:653) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.StreamScanner.parseFullName(StreamScanner.java:1933) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.StreamScanner.parseEntityName(StreamScanner.java:2058) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.StreamScanner.fullyResolveEntity(StreamScanner.java:1525) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.BasicStreamReader.parseAttrValue(BasicStreamReader.java:2017) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.BasicStreamReader.handleNsAttrs(BasicStreamReader.java:3145) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:3043) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2919) [woodstox-core-5.0.3.jar:5.0.3]
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1123) [woodstox-core-5.0.3.jar:5.0.3]
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:197) [jaxb-impl-2.2.3-1.jar:2.2.3]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:366) [jaxb-impl-2.2.3-1.jar:2.2.3]
... 16 more
Is there anything else I need to defined to accept those characters (apart of UTF-8) or is it simply not allowed? 我还需要定义其他任何内容来接受那些字符(属于UTF-8)还是完全不允许使用?
Many thanks in advance! 提前谢谢了!
The issue here was not really the / character, but the & before it. 这里的问题实际上不是/字符,而是前面的&。 / is ok by itself, but & needs to be escaped. /本身可以,但是&需要转义。 I was too focused on the / due to the error message. 由于出现错误消息,我太关注/了。
Escaping the & like that fixed the issue: 转义&可以解决此问题:
<field name = "test" value = "test&/"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.