XML属性的JAXB解组问题 [英] JAXB Unmarshalling issues with XML attributes

查看:62
本文介绍了XML属性的JAXB解组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用XML文件的JAXB解组时遇到问题.我需要获取一些在XML元素上的属性,称为"display_value".这是XML文件的一个小示例:

I am having an issue unmarshalling with JAXB for an XML file. There is an attribute on some of the xml elements called "display_value" which I need to obtain. Here is a small example of the XML file:

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

我假设在ChangeRequest类中,我将简单地在具有display_value那个属性的字段(例如alt_poc)上注释@XmlAttribute(name ="display_value). ChangeRequest类.

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.

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

我确实有一个包含ChangeRequest对象列表的类,称为ChangeRequests.此类很简单,看起来像:

I do have a class that contains a list of ChangeRequest objects, called 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;
}

最后,我将向您展示执行所有这些操作的JAXB代码

Finally I will show you the JAXB code where I do all of this

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

当前,所有代码都可以正常工作,但是我无法在需要时获取display_value.而不是display_value,我得到的长号如65484435487.

Currently, all the code works, however I cannot get the display_value when I need it. Instead of the display_value I am getting the long number like 65484435487.

任何人都可以提供的帮助将是巨大的.谢谢!

Any help anyone can provide would be great. Thank you!

推荐答案

您需要为每个具有XML属性的元素创建单独的类,不能在ChangeRequest类上定义display_value.这是一个例子.

You need to create separate classes for each element that has XML attributes, you cannot define display_value on the ChangeRequest class. Here is an example.

首先,我从您的示例XML生成了XSD:

First I generated an XSD from your example XML:

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

然后我从此XSD生成了JAXB类.这是ChangeRequestType:

Then I generated JAXB classes from this XSD. Here is the 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
}

例如,这里是AssignedToType.注意,必须在此处定义display_value:

And here is AssignedToType, for example. Notice display_value must be defined here:

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

这篇关于XML属性的JAXB解组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆