【发布时间】:2018-06-24 08:17:30
【问题描述】:
我正在尝试在 java 中使用 JAXB 创建 XML。这样做时我面临着异常:
有两个名为“title”的属性 此问题与以下位置有关: 在公共 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>
【问题讨论】:
-
您可能想要更改此注释 @XmlAccessorType (XmlAccessType.FIELD),因为它告诉 JAXB 使用您的变量来创建 XML 片段,但同时您正在注释 getter
-
@PillHead 我试过没有这个注释,仍然得到同样的异常。如果每个元素内部有多个子元素,那么只有我面对它。如果我在
标记中只保留一个元素,那么代码可以正常工作,但是当我尝试两个子元素时会发生此异常。 -
需要我们阅读和理解的代码很多,而且缩进很差。这个问题对一个更小的例子有意义吗?见How to create a Minimal, Complete, and Verifiable example。
-
一个细节:
SimpleDateFormat类早已过时并且出了名的麻烦,Date同样过时。我建议你把它们扔掉,改用java.time, the modern Java date and time API,。