英文:
Jackson XML with different element name and attributes under root tag
问题
我对Jackson XML还不熟悉,我有一个要求,需要构造一个具有不同元素名称和属性但位于同一根元素下的Jackson XML。
**我期望的xml输出**
<item name="Whatever">
<problem_id id="12312"/>
<problem_type type="1765"/>
<problem_desc desc="faulty"/>
</item>
**我的POJO类**(不确定如何添加剩余的元素和属性)
```java
@JacksonXmlRootElement(localName = "item")
public class ItemsDTO {
@JacksonXmlProperty(localName = "name",isAttribute = true)
private String name="Whatever";
}
非常感谢任何建议。
<details>
<summary>英文:</summary>
I'm new to Jackson XML and I have a requirement of constructing a Jackson XML with different element name and attributes but under same root element.
**My Expected xml output**
<item name="Whatever">
<problem_id id="12312"/>
<problem_type type="1765"/>
<problem_desc desc="faulty"/>
</item>
**My pojo class** ( Not sure how to add the remaining elements and attributes)
@JacksonXmlRootElement(localName = "item")
public class ItemsDTO {
@JacksonXmlProperty(localName = "name",isAttribute = true)
private String name="Whatever";
}
Any advise would be much appreciated.
</details>
# 答案1
**得分**: 0
为了实现这一点,您可能需要像您已经做过的那样实现更多的类,然后将相关的属性添加到您的容器类`ItemsDTO`中:
```java
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "item")
public class ItemsDTO {
@JacksonXmlProperty(isAttribute = true)
private String name = "Whatever";
@JacksonXmlProperty(localName = "problem_id")
private ProblemId problemId = new ProblemId();
@JacksonXmlProperty(localName = "problem_type")
private ProblemType problemType = new ProblemType();
@JacksonXmlProperty(localName = "problem_desc")
private ProblemDesc problemDesc = new ProblemDesc();
}
class ProblemId {
@JacksonXmlProperty(isAttribute = true)
private int id = 12312;
}
class ProblemType {
@JacksonXmlProperty(isAttribute = true)
private int type = 1765;
}
class ProblemDesc {
@JacksonXmlProperty(isAttribute = true)
private String desc = "faulty";
}
英文:
To do this, you may need to implement more classes like you have already done and then add related properties to your container class ItemsDTO
:
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "item")
public class ItemsDTO {
@JacksonXmlProperty(isAttribute = true)
private String name = "Whatever";
@JacksonXmlProperty(localName = "problem_id")
private ProblemId problemId = new ProblemId();
@JacksonXmlProperty(localName = "problem_type")
private ProblemType problemType = new ProblemType();
@JacksonXmlProperty(localName = "problem_desc")
private ProblemDesc problemDesc = new ProblemDesc();
}
class ProblemId {
@JacksonXmlProperty(isAttribute = true)
private int id = 12312;
}
class ProblemType {
@JacksonXmlProperty(isAttribute = true)
private int type = 1765;
}
class ProblemDesc {
@JacksonXmlProperty(isAttribute = true)
private String desc = "faulty";
}
答案2
得分: 0
虽然将 problem
的属性“封装”在一个单独的元素中,以更短的 XML 输出为佳,就像这样:
<item name="Whatever">
<problem id="12312" type="1765" desc="faulty"/>
</item>
可以通过以下代码实现:
@JacksonXmlRootElement(localName = "item")
public class ItemDTO {
@JacksonXmlProperty(isAttribute = true)
private String name = "Whatever";
@JacksonXmlProperty
private Problem problem = new Problem();
}
class Problem {
@JacksonXmlProperty(isAttribute = true)
private int id = 12312;
@JacksonXmlProperty(isAttribute = true)
private int type = 1765;
@JacksonXmlProperty(isAttribute = true)
private String desc = "faulty";
}
英文:
Although it would be better to have a shorter XML output with problem
's properties "encapsulated" in a single element like this:
<item name="Whatever">
<problem id="12312" type="1765" desc="faulty"/>
</item>
This can be achieved with the following code:
@JacksonXmlRootElement(localName = "item")
public class ItemDTO {
@JacksonXmlProperty(isAttribute = true)
private String name = "Whatever";
@JacksonXmlProperty
private Problem problem = new Problem();
}
class Problem {
@JacksonXmlProperty(isAttribute = true)
private int id = 12312;
@JacksonXmlProperty(isAttribute = true)
private int type = 1765;
@JacksonXmlProperty(isAttribute = true)
private String desc = "faulty";
}
专注分享java语言的经验与见解,让所有开发者获益!
评论