Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add xml attributes to jaxb annotated class XmlElementWrapper?

Tags:

I have a class with a XmlElementWrapper annotation like:

...

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

... This code produces XML like

<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

<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?

like image 742
ABX Avatar asked Sep 08 '10 09:09

ABX


People also ask

How does JAXB read XML?

To read XML, first get the JAXBContext . It is entry point to the JAXB API and provides methods to unmarshal, marshal and validate operations. Now get the Unmarshaller instance from JAXBContext . It's unmarshal() method unmarshal XML data from the specified XML and return the resulting content tree.

What is XML accessor type?

Annotation Type XmlAccessorTypeControls whether fields or Javabean properties are serialized by default. @XmlAccessorType annotation can be used with the following program elements: package. a top level class.

Which tag represents the root element for the XML document in JAXB?

JAXB @XmlRootElement annotation type @XmlRootElement maps a class or an enum type to an XML element. When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.


1 Answers

I got a better solution for your question.

For making Xml Java object, use the following code:

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:

   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 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..

like image 71
Noby George Avatar answered Sep 24 '22 00:09

Noby George