簡體   English   中英

在Java中使用JAXB生成XML

[英]Generating XML using JAXB in Java

我試圖在Java中使用JAXB創建XML。 在這樣做的時候,我面臨着一個例外:

有兩個名為“ title”的屬性,此問題與以下位置有關:在public java.lang.String generateXML.Pojo.getTitle()在generateXML.Pojo處,此問題與以下位置有關:在私有java.lang上。出現在generateXML.Pojo處的字符串generateXML.Pojo.title屬性值存在,但未在@ XmlType.propOrder中指定。此問題與以下位置有關:

下面是pojo和主類的代碼。 有人可以解釋一下為什么會出現這種異常嗎? 還附帶了我需要生成的XML。

   @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;
}
}

這是主要的類:

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();

   }
 }

XML需要以這種方式生成:

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

下面是給出類似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. 護理級別

      @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. 事件通知

        @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; } } 

使用您的主要課程結果是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM