Search code examples
javaxmleclipselinkjavabeansmoxy

Make custom xml from javabean


I wants to make xml from javabean like below:

        <tag2>message</tag2>
        <tag3>message</tag3>
        <tag4 id='UNIQUE MT ID 1'>MOBILE No.</tag4>

I tried below code in javabean:

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "name", propOrder = {"tag2", "tag3", "tag4"})
 public class newBean {

@XmlElement(required = true)
    private List<String> tag2;

@XmlElement(required = true)
    private List<String> tag3;

@XmlElement(required = true)
    private List<String> tag4;

@XmlPath("tag4/@id")
    private List<String> id;

public  List<String> getTag2() {
    return tag2;
}

public void setTag2(List<String> tag2) {
    this.tag2 = tag2;
}

public List<String> gettag4() {
    return tag4;
}

public void settag4(List<String> tag4) {
    this.tag4 = tag4;
}

public List<String> getId() {
    return id;
}

public void setId(List<String> identifier) {
    this.id = identifier;
}

public  List<String> gettag3() {
    return tag3;
}

public void settag3(List<String> tag3) {
    this.tag3 = tag3;
}
}

I am getting below error:

    Errorcom.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of           IllegalAnnotationExceptions
    Property id is present but not specified in @XmlType.propOrder
    this problem is related to the following location:
            at private java.util.List model.newBean.id
            at model.newBean

Please help me.I am using @XmlPath tag and generating error.I searched alot and found that @XmlPath usage is same as above i used but still getting error.


Solution

  • Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    About @XmlPath

    @XmlPath is a EclipseLink JAXB (MOXy) extension and requires that you are using MOXy as your JAXB provider:

    Valid Use Case #1

    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "name", propOrder = { "tag4", "id" })
    public class newBean {
    
        @XmlElement(required=true)
        String tag4;
    
        @XmlPath("tag4/@id")
        String id;
    
    }
    

    Valid Use Case #2

    import java.util.List;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "name", propOrder = { "tag4", "id" })
    public class newBean {
    
        @XmlPath("tag4/@id")
        List<String> id;
    
    }
    

    Invalid Use Case

    import java.util.List;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "name", propOrder = { "tag4", "id" })
    public class newBean {
    
        @XmlElement(required=true)
        List<String> tag4;
    
        @XmlPath("tag4/@id")
        List<String> id;
    
    }
    

    Mapping Your Use Case

    You could introduce an object that corresponds to the tag4 element that has two properties corresponding to the id attribute and text. This would work with any JAXB (JSR-222) implementation.

    newBean

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "name", propOrder = { "tag4", "id" })
    public class newBean {
    
        List<Tag4> tag4;
    
    }
    

    Tag4

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Tag4 {
    
        @XmlAttribute
        private String id;
    
        @XmlValue
        private String value;
    
    }
    

    For More Information