首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXB UnMarshalling呈现空值(JAXB、Java1.8)

JAXB UnMarshalling呈现空值(JAXB、Java1.8)
EN

Stack Overflow用户
提问于 2021-11-10 10:04:30
回答 1查看 26关注 0票数 0

我有一个XSD,我是这样写的:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="GTCMessage">
        <xs:annotation>
            <xs:documentation>
                GTCMessage - To Pass Around using JMS.
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="type" type="xs:int" minOccurs="0"/>
                <xs:element name="scope" type="xs:int" minOccurs="0"/>
                <xs:element name="code" type="xs:int" minOccurs="0"/>
                <xs:element name="target" type="xs:string" minOccurs="0"/>
                <xs:element name="message" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
</xs:schema>

现在,我使用CXF maven插件生成JAXB类。我得到了一个类似于JAXB的类(为此使用了一个反编译器):

代码语言:javascript
复制
import com.gmt.provisioning.gtc.xml.messaging.GTCMessage;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.cxf.xjc.runtime.JAXBToStringStyle;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"type", "scope", "code", "target", "message"})
@XmlRootElement(name = "GTCMessage")
public class GTCMessage {
  protected Integer type;
  
  protected Integer scope;
  
  protected Integer code;
  
  protected String target;
  
  protected String message;
  
  public Integer getType() {
    return this.type;
  }
  
  public void setType(Integer value) {
    this.type = value;
  }
  
  public boolean isSetType() {
    return (this.type != null);
  }
  
  public Integer getScope() {
    return this.scope;
  }
  
  public void setScope(Integer value) {
    this.scope = value;
  }
  
  public boolean isSetScope() {
    return (this.scope != null);
  }
  
  public Integer getCode() {
    return this.code;
  }
  
  public void setCode(Integer value) {
    this.code = value;
  }
  
  public boolean isSetCode() {
    return (this.code != null);
  }
  
  public String getTarget() {
    return this.target;
  }
  
  public void setTarget(String value) {
    this.target = value;
  }
  
  public boolean isSetTarget() {
    return (this.target != null);
  }
  
  public String getMessage() {
    return this.message;
  }
  
  public void setMessage(String value) {
    this.message = value;
  }
  
  public boolean isSetMessage() {
    return (this.message != null);
  }
  
  public String toString() {
    return ToStringBuilder.reflectionToString(this, JAXBToStringStyle.MULTI_LINE_STYLE);
  }
}

现在,为了得到它,我编写了一个简单的类,它只接受一个字符串并对其进行解组:

代码语言:javascript
复制
public class Test {

    public static void main (String args[]) {

        String abc = "<GTCMessage><type>1</type><scope>2</scope><code>1</code><message>16365343278450M</message></GTCMessage>";

        GTCMessage aMessage = JAXB.unmarshal(new StringReader(abc), GTCMessage.class);

        System.out.println(aMessage.getMessage());
    }
}

但是最后一行打印的是null。我原以为它会打印值16365343278450M。事实上,aMessage对象中的每个值都是空的(作用域、类型等)。

我怀疑我写的XSD可能出了什么问题,导致它像多米诺骨牌效应一样出错。

任何提示都会很有帮助。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-10 12:58:40

我能自己修好它。有两种方法可以做到这点。

首先是更改字符串,如下所示:

代码语言:javascript
复制
String abc = "<GTCMessage xmlns=\"http://www.gmt.com/provisioning/gtc/xml/Messaging\"><type>1</type><scope>2</scope><code>1</code><message>16365343278450M</message></GTCMessage>";

或者更改XSD中的XSD elementFormDefault,并保留不带名称空间的原始字符串。

代码语言:javascript
复制
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="unqualified" attributeFormDefault="unqualified">

我选择了后者,因为它对我来说更容易管理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69911241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档