【问题标题】:Unmarshall with JAXB doesn't work使用 JAXB 解组不起作用
【发布时间】:2014-12-25 11:28:26
【问题描述】:

我有一个简单的 XML,我想将它解组为一个模型类。我已经使用 JAXB 注释对类进行了注释,用于定义访问类型(FIELD):

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD)
public class DtoTest {

    private String name;

    public DtoTest() {}

    public DtoTest(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "DtoTest [name=" + name + "]";
    }
}

这是我的主类,我在其中针对保存在 String 变量中的简单 XML 运行解组方法:

public class Test {

    public static void main(String[] args) throws Exception {
        Object obj = new DtoTest();
        String testXML = "<dtoTest><name>example</name></dtoTest>";
        obj = unmarshal(obj, testXML);
        System.out.println(obj);
    }

    /* This is a generic unmarshall method which I've already used with success with other XML*/
    public static <T> T unmarshal(T obj, String xml) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));

        Class<? extends Object> type = obj.getClass();
        JAXBContext jc = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        obj =  (T)unmarshaller.unmarshal(xsr, type).getValue();
        xsr.close();

        return obj;
    }
}

每当我运行代码时,我都会得到相同的输出:

DtoTest [name=null]

我不明白我做错了什么。

【问题讨论】:

  • 我在我的简单项目中尝试了您的示例,并且工作正常。 :) 你可以添加所有库的版本吗? (输出是 DtoTest [name=example])我只使用了差异XMLInputFactory xif = XMLInputFactory.newInstance();
  • 我在一个使用 JavaSE 1.7 的简单 java 项目中运行此代码,我没有配置任何额外的 jar 或依赖项。
  • 我发现同一个包中有一个包信息类导致了奇怪的行为。删除后,一切正常。

标签: java xml jaxb


【解决方案1】:

我刚刚在 jdk1.7.0_67 上运行了你的代码,它可以工作。

DtoTest [name=example]

也许您对包含的库有一些问题?我只用普通的java运行它。

【讨论】:

    【解决方案2】:

    你的问题对我来说完全没问题。您可以对其进行的一项优化是创建StreamSource 而不是XMLStreamReader

    import javax.xml.bind.*;
    import javax.xml.transform.stream.StreamSource;
    import java.io.StringReader;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            Object obj = new DtoTest();
            String testXML = "<dtoTest><name>example</name></dtoTest>";
            obj = unmarshal(obj, testXML);
            System.out.println(obj);
        }
    
        public static <T> T unmarshal(T obj, String xml) throws Exception {
            StreamSource source = new StreamSource(new StringReader(xml));
    
            Class<? extends Object> type = obj.getClass();
            JAXBContext jc = JAXBContext.newInstance(type);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            obj =  (T)unmarshaller.unmarshal(source, type).getValue();
    
            return obj;
        }
    
    }
    

    调试提示

    当解组未按预期工作时,填充您的 JAXB 模型并将其编组为 XML 以查看预期 XML 的样子。

    【讨论】:

    • 我发现同一个包中有一个包信息类导致了奇怪的行为。我删除它后,一切正常。感谢您的优化提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2020-11-15
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多