【问题标题】:JAXB unmarshalling for complex type with attributes and text content具有属性和文本内容的复杂类型的 JAXB 解组
【发布时间】:2016-02-23 14:24:15
【问题描述】:

我一直在尝试使用 JAXB 解组以下 XML 内容。

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://wso2.org/2016/wso2as-web">
    <Property Key="name">value</Property>
</Root> 

在一些帖子中提到在这种情况下使用@XmlValue 注释来检索文本内容,但由于以下问题,我到目前为止失败了。

If a class has @XmlElement property, it cannot have @XmlValue property

目前我准备的代码如下:

package org.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class Root {
    private Property property;

    public Property getPropertyObject() {
        return property;
    }

    public void setPropertyObject(Property property) {
        this.property = property;
    }

    @XmlRootElement(name = "Property")
    public static class Property {
        @XmlAttribute(name = "Key")
        private String key;
        @XmlValue
        private String text;

        public String getKeyObject() {
            return key;
        }

        public void setKeyObject(String key) {
            this.key = key;
        }

        public String getValueObject() {
            return text;
        }

        public void setValueObject(String value) {
            this.text = value;
        }
    }
}

由于我对 JAXB 比较陌生,因此非常感谢任何有关此方面的帮助。

【问题讨论】:

    标签: java xml jaxb


    【解决方案1】:

    您必须使用@XmlAccessorType(XmlAccessType.FIELD) 注释Propertyclass。

    否则,它的 getXxx() 方法被视为元素,因为 getter 的名称与字段的名称不匹配。

    【讨论】:

      【解决方案2】:

      想补充本杰明的答案,你的例外是由于内部类Property 没有注释 @XmlAccessorType(XmlAccessType.NONE)@XmlAccessorType(XmlAccessType.FIELD)

      【讨论】:

      • @Benjamin 有道理。已编辑。
      【解决方案3】:

      我找到了上述案例的答案。以上两个答案确实解决了弹出的异常问题(在帖子中提到)但没有加载任何与无法正常工作的加载相关的内容。

      这里是固定代码:

      package org.test;
      
      import javax.xml.bind.annotation.XmlAccessType;
      import javax.xml.bind.annotation.XmlAccessorType;
      import javax.xml.bind.annotation.XmlAttribute;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlValue;
      
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlRootElement(name = "Root")
      public class Root {
          @XmlElement(name = "Property")
          private Property property;
      
          public Property getPropertyObject() {
              return property;
          }
      
          public void setPropertyObject(Property property) {
              this.property = property;
          }
      
          @XmlAccessorType(XmlAccessType.FIELD)
          public static class Property {
              @XmlAttribute(name = "Key")
              private String key;
              @XmlValue
              private String text;
      
              public String getKeyObject() {
                  return key;
              }
      
              public void setKeyObject(String key) {
                  this.key = key;
              }
      
              public String getValueObject() {
                  return text;
              }
      
              public void setValueObject(String value) {
                  this.text = value;
              }
          }
      }
      

      我从嵌套的 Property 类中删除了 @XmlRootElement(name = "Property") 注释,并将 @XmlElement(name = "Property") 添加到 Root.java 的 Property 实例变量中。

      【讨论】:

        猜你喜欢
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多