我目前正在开发一个Java Web应用程序,它在某个时候从数据库中读取这个xml数据,并尝试使用Java的 Unmarshaller 类将其转换为Object。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<knowledge idLog="1312" xmlns="http://www.testknowledge.it/knowledge">
<knowledge_request code="REQUEST_CODE">
<par type="continuo" data="08-02-2018 15:42:37" code="requestedfor_1" id="">
<val>ÌÉÀÈÉĒ</val>
</par>
<par type="continuo" data="08-02-2018 15:42:37" code="requestedfor_2" id="">
<val>ÌÉÀÈÉĒ</val>
</par>
<par type="continuo" data="08-02-2018 15:42:42" code="gender" id="">
<val>M</val>
</par>
</knowledge_request>
</knowledge>
尝试转换存储在此xml中的信息的Java代码是:
public static Object str2obj(String str) throws KnowdledgeException {
try {
InputStream is = new ByteArrayInputStream(str.getBytes());
Object o;
o = unmarshaller.unmarshal(is);
return o;
} catch (JAXBException e) {
throw new KnowdledgeException(e);
}
}
str 的值基本上是从数据库读取的上述xml数据。 和unmarshaller声明:
static {
try {
JAXBContext context = JAXBContext.newInstance("it.health.knowledge.data");
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
unmarshaller = context.createUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException(
"There was a problem creating a JAXBContext object for formatting the object to XML.");
}
}
将函数应用于xml数据时引发的异常是: [org.apache.xerces.impl.io.MalformedByteSequenceException:2字节UTF-8序列的字节2无效。]
对于如何解决这个问题的任何想法表示赞赏!
答案 0 :(得分:0)
使用unmarshaller.unmarshal(new StringReader(str))
。