使用Java中的JAXB生成XML

时间:2018-01-15 16:50:49

标签: java xml jaxb

我正在尝试使用Java中的JAXB创建XML。在这样做的同时,我面临例外:

有两个名为“title”的属性     此问题与以下位置有关:         at public java.lang.String generateXML.Pojo.getTitle()         在generateXML.Pojo     此问题与以下位置有关:         在私人java.lang.String generateXML.Pojo.title         在generateXML.Pojo 属性值存在但未在@ 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>

1 个答案:

答案 0 :(得分:0)

这是一个给出类似xml的例子:

1.ClientProblemReference

malloc
  1. Carelevel

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

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

      4.Pojo

          @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;
              }
      
          }
      
  2. 使用您的主类结果是:

      @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;
     }
    
    }
    
相关问题