如何向jaxb注释类XmlElementWrapper添加xml属性?

时间:2022-04-25 20:53:36

I have a class with a XmlElementWrapper annotation like:

我有一个类具有XmlElementWrapper注释,比如:

...

  @XmlElementWrapper(name="myList")
    @XmlElements({
    @XmlElement(name="myElement") }
    )
    private List<SomeType> someList = new LinkedList();

... This code produces XML like

…这段代码生成XML。

<myList>
  <myElement> </myElement>
  <myElement> </myElement>
  <myElement> </myElement>
</myList>

so far so good.

目前为止一切都很顺利。

But now I need to add attributes to the list tag to get XML like

但是现在我需要向列表标记添加属性以获得类似于XML的属性

<myList number="2">
  <myElement> </myElement>
  <myElement> </myElement>
  <myElement> </myElement>
</myList>

Is there a 'smart way to achieve this without creating a new class that contains represents the list?

是否有一种“不创建包含列表的新类的智能方法来实现这一点?”

3 个解决方案

#1


25  

I got a better solution for your question.

我得到了一个更好的解决方案。

For making Xml Java object, use the following code:

要创建Xml Java对象,请使用以下代码:

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="myList")
public class Root {

    private String number;
    private List<String> someList;

    @XmlAttribute(name="number")
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @XmlElement(name="myElement")
    public List<String> getSomeList() {
        return someList;
    }

    public void setSomeList(List<String> someList) {
        this.someList = someList;
    } 

    public Root(String numValue,List<String> someListValue) {
        this();
        this.number = numValue;
        this.someList = someListValue;  
    }

    /**
     * 
     */
    public Root() {
        // TODO Auto-generated constructor stub
    }

}

}

To run the above code using JAXB, use the following:

要使用JAXB运行上述代码,请使用以下代码:

   import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.*;

public class Demo {

        public static void main(String[] args) throws Exception {
            List<String> arg = new ArrayList<String>();
            arg.add("FOO");
            arg.add("BAR");
            Root root = new Root("123", arg);

            JAXBContext jc = JAXBContext.newInstance(Root.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.marshal(root, System.out);
        }
}

This will produce the following XML as the output:

这将生成以下XML作为输出:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <myList number="123">
        <myElement>FOO</myElement>
        <myElement>BAR</myElement>
    </myList>

I think this is more helpful you.

我认为这对你更有帮助。

Thanks..

谢谢. .

#2


11  

The MOXy JAXB implementation (I'm the tech lead) has an extension (@XmlPath) to handle this case:

MOXy JAXB实现(我是技术负责人)有一个扩展(@XmlPath)来处理这种情况:

import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlPath("myList/@number")
    private int number;

    @XmlElementWrapper(name="myList") 
    @XmlElement(name="myElement") 
    private List<String> someList = new LinkedList<String>();

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public List<String> getSomeList() {
        return someList;
    }

    public void setSomeList(List<String> someList) {
        this.someList = someList;
    } 

}

Will produce the following XML:

将生成以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <myList number="123">
      <myElement>FOO</myElement>
      <myElement>BAR</myElement>
   </myList>
</root>

When this code is run:

运行此代码时:

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.setNumber(123);
        root.getSomeList().add("FOO");
        root.getSomeList().add("BAR");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }
}

To get this to work using strictly standard JAXB code you will need to use an XML Adapter:

要使用严格标准的JAXB代码来实现这一点,您需要使用XML适配器:

Note:

注意:

To use MOXy JAXB you need to add a file called jaxb.properties in with your model classes with the following entry:

要使用MOXy JAXB,需要添加一个名为JAXB的文件。与您的模型类一起的属性,包括以下条目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

#3


4  

If you are not using MOXy or just want to stick to standard JAXB annotations, you can extend upon Noby's answer to add support for a generic wrapper class. Noby's answer only currently supports a list of strings, but say for example you're going to be using the same generic wrapper class for several different classes. In my example, I want to create a generic "PagedList" class that will marshall to something that looks like a list, but also contains information about the page offset and the total number of elements in unpaged list.

如果您不使用MOXy或只想使用标准的JAXB注释,您可以根据Noby的回答添加对通用包装类的支持。Noby的答案目前只支持字符串列表,但是举例来说,您将为几个不同的类使用相同的泛型包装器类。在我的示例中,我想创建一个通用的“PagedList”类,这个类将绑定到看起来像列表的东西,但也包含关于页面偏移量和未分页列表中的元素总数的信息。

The one downside of this solution is that you have to add additional @XmlElement mappings for each type of class that will be wrapped. Overall though, probably a better solution than creating a new class for each pagable elements.

这个解决方案的一个缺点是必须为将要包装的每种类型的类添加额外的@XmlElement映射。总的来说,可能比为每个可分页元素创建一个新类更好的解决方案。

@XmlType
public class PagedList<T> {
    @XmlAttribute
    public int offset;

    @XmlAttribute
    public long total;

    @XmlElements({
        @XmlElement(name="order", type=Order.class),
        @XmlElement(name="address", type=Address.class)
        // additional as needed
    })
    public List<T> items;
}

@XmlRootElement(name="customer-profile")
public class CustomerProfile {
    @XmlElement
    public PagedList<Order> orders;
    @XmlElement
    public PagedList<Address> addresses;
}

Marshalling this example would get you:

编组此示例将使您:

<customer-profile>
    <order offset="1" total="100">
        <order> ... </order>
        <order> ... </order>
        <order> ... </order>
        ...
    </orders>
    <addresses offset="1" total="5">
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
    <addresses>
</customer-profile>

Hope that helps. This is the solution that I settled upon at least.

希望有帮助。这至少是我确定的解决办法。

#1


25  

I got a better solution for your question.

我得到了一个更好的解决方案。

For making Xml Java object, use the following code:

要创建Xml Java对象,请使用以下代码:

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="myList")
public class Root {

    private String number;
    private List<String> someList;

    @XmlAttribute(name="number")
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @XmlElement(name="myElement")
    public List<String> getSomeList() {
        return someList;
    }

    public void setSomeList(List<String> someList) {
        this.someList = someList;
    } 

    public Root(String numValue,List<String> someListValue) {
        this();
        this.number = numValue;
        this.someList = someListValue;  
    }

    /**
     * 
     */
    public Root() {
        // TODO Auto-generated constructor stub
    }

}

}

To run the above code using JAXB, use the following:

要使用JAXB运行上述代码,请使用以下代码:

   import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.*;

public class Demo {

        public static void main(String[] args) throws Exception {
            List<String> arg = new ArrayList<String>();
            arg.add("FOO");
            arg.add("BAR");
            Root root = new Root("123", arg);

            JAXBContext jc = JAXBContext.newInstance(Root.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.marshal(root, System.out);
        }
}

This will produce the following XML as the output:

这将生成以下XML作为输出:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <myList number="123">
        <myElement>FOO</myElement>
        <myElement>BAR</myElement>
    </myList>

I think this is more helpful you.

我认为这对你更有帮助。

Thanks..

谢谢. .

#2


11  

The MOXy JAXB implementation (I'm the tech lead) has an extension (@XmlPath) to handle this case:

MOXy JAXB实现(我是技术负责人)有一个扩展(@XmlPath)来处理这种情况:

import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlPath("myList/@number")
    private int number;

    @XmlElementWrapper(name="myList") 
    @XmlElement(name="myElement") 
    private List<String> someList = new LinkedList<String>();

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public List<String> getSomeList() {
        return someList;
    }

    public void setSomeList(List<String> someList) {
        this.someList = someList;
    } 

}

Will produce the following XML:

将生成以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <myList number="123">
      <myElement>FOO</myElement>
      <myElement>BAR</myElement>
   </myList>
</root>

When this code is run:

运行此代码时:

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.setNumber(123);
        root.getSomeList().add("FOO");
        root.getSomeList().add("BAR");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }
}

To get this to work using strictly standard JAXB code you will need to use an XML Adapter:

要使用严格标准的JAXB代码来实现这一点,您需要使用XML适配器:

Note:

注意:

To use MOXy JAXB you need to add a file called jaxb.properties in with your model classes with the following entry:

要使用MOXy JAXB,需要添加一个名为JAXB的文件。与您的模型类一起的属性,包括以下条目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

#3


4  

If you are not using MOXy or just want to stick to standard JAXB annotations, you can extend upon Noby's answer to add support for a generic wrapper class. Noby's answer only currently supports a list of strings, but say for example you're going to be using the same generic wrapper class for several different classes. In my example, I want to create a generic "PagedList" class that will marshall to something that looks like a list, but also contains information about the page offset and the total number of elements in unpaged list.

如果您不使用MOXy或只想使用标准的JAXB注释,您可以根据Noby的回答添加对通用包装类的支持。Noby的答案目前只支持字符串列表,但是举例来说,您将为几个不同的类使用相同的泛型包装器类。在我的示例中,我想创建一个通用的“PagedList”类,这个类将绑定到看起来像列表的东西,但也包含关于页面偏移量和未分页列表中的元素总数的信息。

The one downside of this solution is that you have to add additional @XmlElement mappings for each type of class that will be wrapped. Overall though, probably a better solution than creating a new class for each pagable elements.

这个解决方案的一个缺点是必须为将要包装的每种类型的类添加额外的@XmlElement映射。总的来说,可能比为每个可分页元素创建一个新类更好的解决方案。

@XmlType
public class PagedList<T> {
    @XmlAttribute
    public int offset;

    @XmlAttribute
    public long total;

    @XmlElements({
        @XmlElement(name="order", type=Order.class),
        @XmlElement(name="address", type=Address.class)
        // additional as needed
    })
    public List<T> items;
}

@XmlRootElement(name="customer-profile")
public class CustomerProfile {
    @XmlElement
    public PagedList<Order> orders;
    @XmlElement
    public PagedList<Address> addresses;
}

Marshalling this example would get you:

编组此示例将使您:

<customer-profile>
    <order offset="1" total="100">
        <order> ... </order>
        <order> ... </order>
        <order> ... </order>
        ...
    </orders>
    <addresses offset="1" total="5">
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
        <address> ... </address>
    <addresses>
</customer-profile>

Hope that helps. This is the solution that I settled upon at least.

希望有帮助。这至少是我确定的解决办法。