【问题标题】:Generating XML using JAXB in Java在 Java 中使用 JAXB 生成 XML
【发布时间】: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,

标签: java xml jaxb


【解决方案1】:

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

1.客户端问题参考

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2011-08-27
    • 1970-01-01
    • 2012-04-10
    相关资源
    最近更新 更多