我在将 xml 转换为 java 类时遇到问题,无法看到问题。
public MyResponse getSmartFocusResponse(MyResponse myResponse, String data) throws JAXBException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(myResponse.getClass());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(data);
MyResponse response = (MyResponse) unmarshaller.unmarshal(reader);
return response;
} catch (JAXBException e) {
e.printStackTrace();
throw e;
}
}
这是我转换为的类:
@XmlRootElement(name = "response")
public class MyResult {
private InnerResult result;
private String responseStatus;
@XmlElement(name = "result")
public void setResult(InnerResult uploadStatus) {
this.result = uploadStatus;
}
@XmlAttribute(name = "responseStatus")
public void setResponseStatus(String responseStatus) {
this.responseStatus = responseStatus;
}
public InnerResult getResult() {
return result;
}
public String getResponseStatus() {
return responseStatus;
}
}
@XmlRootElement(name = "result")
public class InnerResult {
private String content;
private String attribute;
@XmlAttribute(name = "xsi:type")
public void setAttribute(String attribute) {
this.attribute = attribute;
}
@XmlElement
public void setContent(String content) {
this.content = content;
}
public String getAttribute() {
return attribute;
}
public String getContent() {
return content;
}
}
最后,这是我尝试转换的 XML:
<response responseStatus="success">
<result xsi:type="xs:string">
connection closed
</result>
</response>
我捕获的异常是:
javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 120; The prefix "xsi" for attribute "xsi:type" associated with an element type "result" is not bound.]
最佳答案
问题
以下 XML 文档对于命名空间感知解析器无效,因为它期望存在与前缀“xsi”关联的命名空间:
<response responseStatus="success">
<result xsi:type="xs:string">
connection closed
</result>
</response>
下面是 XML 解析器想要的:
<response responseStatus="success" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result xsi:type="xs:string">
connection closed
</result>
</response>
修复
I cannot change the XML, its an input from 3d party...
由于您无法更改 XML,因此您需要使用不支持命名空间的 XML 解析器。好消息是 JDK/JRE 中有一个。
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
JAXBContext jc = JAXBContext.newInstance(MyResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
xr.setContentHandler(unmarshallerHandler);
String data = ...;
StringReader reader = new StringReader(data);
InputSource xmlSource = new InputSource(reader);
xr.parse(xmlSource);
MyResponse myResponse = (MyResponse) unmarshallerHandler.getResult();
}
}
关于java - 无法将 xml 转换为 jaxb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35064710/