简体   繁体   English

JAXB Unmarshalling XML属性的问题

[英]JAXB Unmarshalling issues with XML attributes

I am having an issue unmarshalling with JAXB for an XML file. 我正在解决使用JAXB解析XML文件的问题。 There is an attribute on some of the xml elements called "display_value" which I need to obtain. 我需要获取一些名为“display_value”的xml元素的属性。 Here is a small example of the XML file: 以下是XML文件的一个小示例:

<unload>
  <change_request>
    <active>true</active>
    <approval>not requested</approval>
    <assigned_to display_value=""/>
    <alt_poc display_value="Tom Ford">056468745677484657</alt_poc>
    <poc display_value="Matt Ryan">56465148754878</poc>
   </change_request>
</unload>

I assume that in my ChangeRequest class I would simply annotate @XmlAttribute(name="display_value) on the fields that have the display_value that attribute, such as alt_poc but that doesn't seem to work. Here is an example of my ChangeRequest class. 我假设在我的ChangeRequest类中,我只是在具有display_value属性的字段上注释@XmlAttribute(name =“display_value),例如alt_poc,但似乎不起作用。这是我的ChangeRequest类的一个例子。

@XmlAccessorType(XmlAccessType.FIELD)
public class ChangeRequest{
  String active;
  String approval;
  String assigned_to;
  String alt_poc;
  String poc;
}

I do have a class that contains a list of ChangeRequest objects, called ChangeRequests. 我有一个包含ChangeRequest对象列表的类,名为ChangeRequests。 This class is simple and looks like: 这个类很简单,看起来像:

@XmlRootElement(name="unload")
public class ChangeRequests{
ArrayList<ChangeRequest> changeRequestList;

@XmlElement(name="change_request")
public ArrayList<ChangeRequest> getRecords(){
  return changeRequestList;
}

Finally I will show you the JAXB code where I do all of this 最后,我将向您展示JAXB代码,其中我将完成所有这些操作

URL url = new URL("wwww.somethingInteresting.com/syz.xml");
try {
   JAXBConext jc = JAXBContext.newInstance(ChangeRequest.class, ChangeRequests.class);
   Unmarshaller un  = jc.createUnmarshaller();

   return (ChangeRequests) un.unmarshal(url);
} catch(JAXBException e){
    thow new RunTimeException(e);
}

Currently, all the code works, however I cannot get the display_value when I need it. 目前,所有代码都有效,但是当我需要它时,我无法获得display_value。 Instead of the display_value I am getting the long number like 65484435487. 而不是display_value我得到像65484435487这样的长数字。

Any help anyone can provide would be great. 任何人都可以提供任何帮助都会很棒。 Thank you! 谢谢!

You need to create separate classes for each element that has XML attributes, you cannot define display_value on the ChangeRequest class. 您需要为具有XML属性的每个元素创建单独的类,您不能在ChangeRequest类上定义display_value。 Here is an example. 这是一个例子。

First I generated an XSD from your example XML: 首先,我从您的示例XML生成了一个XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="unload" type="unloadType"/>
  <xs:complexType name="alt_pocType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="display_value"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="change_requestType">
    <xs:sequence>
      <xs:element type="xs:string" name="active"/>
      <xs:element type="xs:string" name="approval"/>
      <xs:element type="assigned_toType" name="assigned_to"/>
      <xs:element type="alt_pocType" name="alt_poc"/>
      <xs:element type="pocType" name="poc"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="assigned_toType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="display_value"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="pocType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="display_value"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="unloadType">
    <xs:sequence>
      <xs:element type="change_requestType" name="change_request"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Then I generated JAXB classes from this XSD. 然后我从这个XSD生成了JAXB类。 Here is the ChangeRequestType: 这是ChangeRequestType:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "change_requestType", propOrder = {
    "active",
    "approval",
    "assignedTo",
    "altPoc",
    "poc"
})
public class ChangeRequestType {

    @XmlElement(required = true)
    protected String active;
    @XmlElement(required = true)
    protected String approval;
    @XmlElement(name = "assigned_to", required = true)
    protected AssignedToType assignedTo;
    @XmlElement(name = "alt_poc", required = true)
    protected AltPocType altPoc;
    @XmlElement(required = true)
    protected PocType poc;

  // Getters and setters follow
}

And here is AssignedToType, for example. 例如,这里是AssignedToType。 Notice display_value must be defined here: 注意display_value必须在这里定义:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "assigned_toType", propOrder = {
        "value"
    })
    public class AssignedToType {

        @XmlValue
        protected String value;
        @XmlAttribute(name = "display_value")
        protected String displayValue;

        // Getters and setters follow
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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