“Jackson XML在根标签下具有不同的元素名称和属性”

huangapple 未分类评论81阅读模式
英文:

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 = &quot;item&quot;)
public class ItemsDTO {

	 @JacksonXmlProperty(localName = &quot;name&quot;,isAttribute = true)
	 private String name=&quot;Whatever&quot;;

}

非常感谢任何建议。



<details>
<summary>英文:</summary>

I&#39;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 = &quot;name&quot;,isAttribute = true)
 private String name=&quot;Whatever&quot;;

}


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 = &quot;item&quot;)
public class ItemsDTO {

    @JacksonXmlProperty(isAttribute = true)
    private String name = &quot;Whatever&quot;;

    @JacksonXmlProperty(localName = &quot;problem_id&quot;)
    private ProblemId problemId = new ProblemId();

    @JacksonXmlProperty(localName = &quot;problem_type&quot;)
    private ProblemType problemType = new ProblemType();

    @JacksonXmlProperty(localName = &quot;problem_desc&quot;)
    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 = &quot;faulty&quot;;
}

答案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:

&lt;item name=&quot;Whatever&quot;&gt;
  &lt;problem id=&quot;12312&quot; type=&quot;1765&quot; desc=&quot;faulty&quot;/&gt;
&lt;/item&gt;

This can be achieved with the following code:

@JacksonXmlRootElement(localName = &quot;item&quot;)
public class ItemDTO {

    @JacksonXmlProperty(isAttribute = true)
    private String name = &quot;Whatever&quot;;

    @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 = &quot;faulty&quot;;
}

huangapple
  • 本文由 发表于 2020年4月10日 21:49:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61141672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定