XML 属性的 JAXB 解组问题

JAXB Unmarshalling issues with XML attributes(XML 属性的 JAXB 解组问题)

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

问题描述

我在使用 JAXB 解组 XML 文件时遇到问题.我需要获取一些名为display_value"的 xml 元素的属性.下面是 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 字段上注释 @XmlAttribute(name="display_value),例如 alt_poc,但这似乎不起作用.这是我的一个示例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 解组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:XML 属性的 JAXB 解组问题

基础教程推荐