【发布时间】: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 并剪切第二行,但这不是问题。现在我什至不知道我可以做些什么不同,要改变什么以及如何检查哪里可能出现问题。 我将不胜感激任何建议,想法。谢谢!
【问题讨论】:
-
您是否尝试过此答案中描述的方法 - stackoverflow.com/a/47040399/2792888 ?
-
是的,不工作:(