【问题标题】:Unmarshall xml - the main class get nullUnmarshall xml - 主类为空
【发布时间】:2020-02-23 17:28:31
【问题描述】:

我有需要解组的 xml:

 <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE nitexp SYSTEM "http://www.nitml.org/nitexp/1.0/nitexp.dtd" [<!ENTITY % nitcml SYSTEM "http://www.nitml.org/nitcml/1.0/nitcml.dtd" > %nitcml;]>
    <nitexp xmlns="http://www.nitml.org/nitexp/1.0">
    <item type="sasa">
    <nd>nitl20043581</nd>
    <source>
    <name>name</name>
    <issue>
    <publicdate>20200210</publicdate>
    <page>32</page>
    <column></column>
    </issue>
    </source>
    <title>Immof inanz </title>
    <author>hshsa</author>
    <annotation>
    <nitcml xmlns="http://www.nitml.org/nitcml/1.0">
    </nitcml>
    </annotation>
    <content>
    <nitcml xmlns="http://www.nitml.org/nitcml/1.0">
    <body><p><l>dsadasdasd</l><l>dasdasd</l><l> dasdas</l><l></l></p></body>
    </nitcml>
    </content>
    <nmms><importdate>20200210</importdate>
    <topic code="np01"/>
    <mark> </mark>
    <useraction></useraction>
    <weight></weight>
    <group></group><attachments></attachments><note>
    <nitcml xmlns="http://www.nitml.org/nitcml/1.0"></nitcml></note>
    <translation><nitcml xmlns="http://www.nitml.org/nitcml/1.0"></nitcml>
    </translation>
    <field>
    <value>ni</value>
    <value>20a04</value>
    <value>358d1</value>
    </field>
    </nmms>
    </item>
    </nitexp>

我可以轻松编组它,但是当我在另一边尝试时,我得到了 null 项目。我的主要课程是:

  @Builder
@AllArgsConstructor
@NoArgsConstructor
@Setter
@ToString
@XmlRootElement(name = "nitexp", namespace="http://www.nitml.org/nitexp/1.0")
public class Article {

    private Item item = Item.builder().build();

    @XmlElement(name = "item")
    public ArticleItem getItem() {
        return item;
    }

}

然后

  @Builder
@AllArgsConstructor
@NoArgsConstructor
@Setter
@ToString
@XmlType(propOrder={"type","nitoid","source","title","author","annotation","content","nmms"})
public class ArticleItem {

    private String type;
    private String nitoid;
    private ArticleSource source = new ArticleSource();
    private String title;
    private String author;
    private String annotation;
    private String content;
    private String nmms;


    @XmlAttribute(name = "type")
    public String getType() {
        return type;
    }

...等等..

我使用 Java 12,我的 pom 依赖项中有 jaxb-api 和 glassfish.jaxb。我试图将 @XMLElement 注释放在 getter 和 setter 上。我试图将 XMLRootElement 注释放在每个嵌套类上,但没有运气。我也尝试不使用@Builder,也不初始化类对象。我什至尝试修改我的 xml 并剪切第二行,但这不是问题。现在我什至不知道我可以做些什么不同,要改变什么以及如何检查哪里可能出现问题。 我将不胜感激任何建议,想法。谢谢!

【问题讨论】:

标签: java xml jaxb


【解决方案1】:

以下设置对我有用:

文章分类:

@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "nitexp", namespace="http://www.nitml.org/nitexp/1.0")
public class Article {

    @XmlElement(name = "item", namespace="http://www.nitml.org/nitexp/1.0")
    private ArticleItem  item;

}

ArticleItem 类:

@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "item", namespace="http://www.nitml.org/nitexp/1.0")
public class ArticleItem {

    @XmlAttribute
    private String type;

    @XmlElement(name = "nd", namespace="http://www.nitml.org/nitexp/1.0")
    private String nitoid;

    @XmlElement(name = "title", namespace="http://www.nitml.org/nitexp/1.0")
    private String title;

    @XmlElement(name = "author", namespace="http://www.nitml.org/nitexp/1.0")
    private String author;

}

解组过程:

    Unmarshaller unmarshaller = JAXBContext.newInstance(Article.class).createUnmarshaller();
    StringReader reader = new StringReader(data);
    Object obj = unmarshaller.unmarshal(new StreamSource(reader));
    System.out.println(obj);

输出:

Article(item=ArticleItem(type=sasa, nitoid=nitl20043581, title=Immof inanz , author=hshsa))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多