简体   繁体   中英

Generating XML using JAXB in Java

I am trying to create a XML using JAXB in java. While doing so I am facing the exception :

There are two properties named "title" this problem is related to the following location: at public java.lang.String generateXML.Pojo.getTitle() at generateXML.Pojo this problem is related to the following location: at private java.lang.String generateXML.Pojo.title at generateXML.Pojo Property value is present but not specified in @XmlType.propOrder this problem is related to the following location:

Below is the code for the pojo's and the main class. Can someone explain me why is this exception coming? Also attached is the XML which I need to generate.

   @XmlRootElement(name ="problem")
   @XmlAccessorType (XmlAccessType.FIELD)
   @XmlType(propOrder={
    "description",
    "intrusiveConsent",
    "title",
    "careLevel",
    "eventNotification",
    "clientProblemReference"
    //"partyList"
          })

public class Pojo {

private String description;
private String intrusiveConsent;
private String title;
private CareLevel careLevel;
private ClientProblemReference clientProblemReference;
private EventNotification eventNotification;
//private PartyList partyList;

@XmlElement(name="S873:description")
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

@XmlElement(name="S873:intrusiveConsent")
public String getIntrusiveConsent() {
    return intrusiveConsent;
}
public void setIntrusiveConsent(String intrusiveConsent) {
    this.intrusiveConsent = intrusiveConsent;
}
@XmlElement(name="S873:title")
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
@XmlElement(name="S873:careLevel")
public CareLevel getCareLevel() {
    return careLevel;
}
public void setCareLevel(CareLevel careLevel) {
    this.careLevel = careLevel;
}

@XmlElement(name="S873:clientProblemReference")
public ClientProblemReference getClientProblemReference() {
    return clientProblemReference;
}
public void setClientProblemReference(ClientProblemReference cpr) {
    clientProblemReference = cpr;
}

@XmlElement(name="S873:eventNotification")
public EventNotification getEventNotification() {
    return eventNotification;
}
public void setEventNotification(EventNotification eventNotification) {
    this.eventNotification = eventNotification;
}
/*@XmlElement(name="S873:partyList")
public PartyList getPartyList() {
    return partyList;
}
public void setPartyList(PartyList partyList) {
    this.partyList = partyList;
}*/

}



@XmlType( propOrder = {"value,type"})

public class ClientProblemReference {
public String value;
    public String type;

@XmlElement(name="S873:value")
public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
@XmlElement(name="S873:type")
public String getType() {
    return type;
}

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

And this is the main class:

public class JAXBExample {
    public static void main(String args[]) throws JAXBException{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm:ss");
String formattedDate = sdf.format(date);

Pojo pj=new Pojo();
CareLevel cl=new CareLevel();
EventNotification event=new EventNotification();
ClientProblemReference cpr=new ClientProblemReference();
/*PartyIdentifier partyIdentifier= new PartyIdentifier();
Party party= new Party();
PartyList partyList= new PartyList();*/
pj.setDescription("Amend");
pj.setIntrusiveConsent("Y");
pj.setTitle("Amend");
cl.setName("Maintenance Category 3");
pj.setCareLevel(cl);
cpr.setValue("267435575");
cpr.setType("amit");
List<ClientProblemReference> al=new ArrayList<ClientProblemReference>();
al.add(cpr);

event.setDate(formattedDate);
pj.setClientProblemReference(cpr);
System.out.println(pj.getClientProblemReference());
pj.setEventNotification(event);
/*partyIdentifier.setName("MaintenanceId");
partyIdentifier.setValue("522695198");
party.setPartyIdentifier(partyIdentifier);
partyList.setParty(party);
pj.setPartyList(partyList);*/
String result=jaxbObjectToXML(pj);
System.out.println(result);

}

   private static String jaxbObjectToXML(Pojo pj) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Pojo.class);
StringWriter stringWriter = new StringWriter();
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(pj, stringWriter);
//System.out.println(stringWriter);
return stringWriter.toString();

   }
 }

The XML needs to be generated in this style:

<problem>
            <!-- Fault description will be sent as Amend as suggested by OS.  It is mapped to "Fault Description" at OS. -->
            <S873:description>Amend</S873:description>
            <!-- "intrusiveConsent" should match with SQ 21CETHC-92 value-->
            <S873:intrusiveConsent>Y</S873:intrusiveConsent>
            <S873:title>Amend</S873:title>

            <S873:careLevel>
            <!-- careLevel/Maintenance category value as received from WCDS (enhance getCustProblemDetails.xml) (currently hardcoded at FM/BZ) -->
                <S873:name>Maintenance Category 3</S873:name>
            </S873:careLevel>
            <S873:clientProblemReference>
            <!-- ESB is expecting CP fault ref in type tag, BZ to send its CP ref value, as exists -->
                <S873:value>267435575</S873:value>
                <S873:type>Amend</S873:type>

Here an example that give a similar xml:

1.ClientProblemReference

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="pbref-type", propOrder = {"value","type"})

public class ClientProblemReference {   
  @XmlElement(name="S873:value")
  public String value;

   @XmlElement(name="S873:type")
   public String type;


    public String getValue() {
     return value;
   }

    public void setValue(String value) {
     this.value = value;
    }

    public String getType() {
      return type;
    } 

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

      @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name="carelevel-type", propOrder = {"name"}) public class CareLevel { @XmlElement(name="S873:name") public String name; public void setName(String name) { // TODO Auto-generated method stub this.name=name; } public String getName() { return name; } } 
    1. EventNotification

        @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name="event-type", propOrder = {"date"}) public class EventNotification { @XmlElement(name="S873:date") String date; public String getDate() { return date; } public void setDate(String date) { // TODO Auto-generated method stub this.date=date; } } 

      4.Pojo

        @XmlRootElement(name ="problem") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder={ "description", "intrusiveConsent", "title", "careLevel", "eventNotification", "clientProblemReference" }) public class Pojo { @XmlElement(name="S873:description") private String description; @XmlElement(name="S873:intrusiveConsent") private String intrusiveConsent; @XmlElement(name="S873:title") private String title; @XmlElement(name="S873:careLevel") private CareLevel careLevel; @XmlElement(name="S873:clientProblemReference") private ClientProblemReference clientProblemReference; @XmlElement(name="S873:eventNotification") private EventNotification eventNotification; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getIntrusiveConsent() { return intrusiveConsent; } public void setIntrusiveConsent(String intrusiveConsent) { this.intrusiveConsent = intrusiveConsent; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public CareLevel getCareLevel() { return careLevel; } public void setCareLevel(CareLevel careLevel) { this.careLevel = careLevel; } public ClientProblemReference getClientProblemReference() { return clientProblemReference; } public void setClientProblemReference(ClientProblemReference cpr) { clientProblemReference = cpr; } public EventNotification getEventNotification() { return eventNotification; } public void setEventNotification(EventNotification eventNotification) { this.eventNotification = eventNotification; } } 

using you main class result is :

                <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                <problem>
                    <S873:description>Amend</S873:description>
                    <S873:intrusiveConsent>Y</S873:intrusiveConsent>
                    <S873:title>Amend</S873:title>
                    <S873:careLevel>
                        <S873:name>Maintenance Category 3</S873:name>
                    </S873:careLevel>
                    <S873:eventNotification>
                        <S873:date>2018-01-15 9:50:57</S873:date>
                    </S873:eventNotification>
                    <S873:clientProblemReference>
                        <S873:value>267435575</S873:value>
                        <S873:type>amit</S873:type>
                    </S873:clientProblemReference>
                </problem>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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