简体   繁体   English

使用JAXB生成xml时如何去除特定元素?

[英]How to remove a specific element when generating xml using JAXB?

I am using JAXB to create xml.我正在使用 JAXB 创建 xml。

It seems that an xml element is created with the current variable name, but the upper list object does not want to be created as an xml element.看起来用当前变量名创建了一个xml元素,但是上面的列表object并不想创建为xml元素。

However, the parent list object is created with a variable name, how to remove the corresponding element?但是父列表object是用变量名创建的,如何去掉对应的元素呢?

Currently my source code is as below.目前我的源代码如下。

XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="POCD_MT000040.Entry", propOrder={"ant123"})
public class POCDMT000040Entry
{
    protected List<DataTest> ant123;

    public List<DataTest> getAnt123() {
    return ant123;
}

public void setAnt123(List<DataTest> ant123) {
    this.ant123 = ant123;
}
}



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="DataTest", propOrder={"datatitle", "data"})
public class DataTest
{
   protected ST datatitle;
   protected Data data;

     public ST getDatatitle() {
    return datatitle;
}

public void setDatatitle(ST datatitle) {
    this.datatitle = datatitle;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}
}

I want xml like below.

<entry>
  <datatitle>1</datatitle>
  <data>1</data>
  <datatitle>2</datatitle>
  <data>2</data>
  <datatitle>3</datatitle>
  <data>3</data>
</entry>

But the xml is being generated like this.

<entry>
 <ant123>
  <datatitle>1</datatitle>
  <data>1</data>
 </ant123>
 <ant123>
  <datatitle>2</datatitle>
  <data>2</data>
 </ant123>
 <ant123>
  <datatitle>3</datatitle>
  <data>3</data>
 </ant123>
</entry>

I want to remove the ant123 element.我想删除 ant123 元素。 Can you tell me how to solve it?你能告诉我如何解决吗?

The items of my ST and Data are as below.我的 ST 和数据的项目如下。

I have described datatitle and data in the description, but the attribute has different values.我在描述中描述了datatitle和data,但是属性有不同的值。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ST", propOrder={"type"})
@XmlSeeAlso({ SC.class, ADXP.class, ENXP.class})
public class ST extends ED {

@XmlAttribute(name = "type")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String type;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Data", propOrder={"type","reference","text"})
@XmlSeeAlso({ SC.class, ADXP.class, ENXP.class})
public class Data extends ANY {


protected POCDMT000040Reference reference;

@XmlAttribute(name = "type")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String type;

protected ST text;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}



public POCDMT000040Reference getReference() {
    return reference;
}

public void setReference(POCDMT000040Reference reference) {
    this.reference = reference;
}

public ST getText() {
    return text;
}

public void setText(ST text) {
    this.text = text;
}

I want xml like below.我想要如下所示的 xml。

<entry>
    <datatitle mediaType="">1</datatitle>
    <data type="TEXT">
        <text>1</text>
    </data>
    <datatitle mediaType="">2</datatitle>
    <data type="TEXT">
        <text>2</text>
    </data>
</entry>

But the xml is being generated like this..但是 xml 是这样生成的。

<entry>
 <any123>
    <datatitle mediaType="">1</datatitle>
    <data type="TEXT">
        <text>1</text>
    </data>
 </any123>
 <any123>
    <datatitle mediaType="">2</datatitle>
    <data type="TEXT">
        <text>2</text>
    </data>
 </any123>
</entry>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM