java - 无法将 xml 转换为 jaxb

标签 java xml jaxb

我在将 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/

相关文章:

java - 在java中搜索二维数组中的单词移动仅是从上到下和从左到右

java - 代码错误(java.awt.image.RasterFormatException)

xml - JAXB/XJC - 从 XS :Group 生成一个类

javascript - 打印每页的销售数量而不是总销售数量

java - 针对泛型和特定类型或该类型列表的灵活 JAXB XML 解析

jaxb - 如何让 JAXB (xjc) 停止在字符和数字之间添加下划线

java - 将 flash 文件合并为 Web 应用程序中的 url 的最佳方法是什么?

java - 如何从jess中的java类中读取变量?

java - Spring aop :config

c# - 操作 XML 文档的属性