【问题标题】:why JAXB java xml unmarshaller return null为什么JAXB java xml unmarshaller返回null
【发布时间】:2019-08-01 15:34:03
【问题描述】:

我正在尝试将 XML 解组为 java 类,如下所示:-

我的xml文件如下:-

<Features_res>
   <StartWeek>202017</StartWeek>
  <EndWeek>202035</EndWeek>
  <Pno12>ABCDEEB</Pno12>

  <FeatureList>
      <Feature>
            <Code>T002</Code>
       </Feature>
         <Feature>
          <Code>T002</Code>
       </Feature>
    </FeatureList>
</Features_res>

Java 内部响应:-

@XmlRootElement(name = "Features_res")
public class InteriorResponse {

@XmlElement(name = "StartWeek")
private int sWeek;
@XmlElement(name = "EndWeek")
private int eWeek;
@XmlElement(name = "Pno12")
private String p12;
List<Feature> featureList;

public InteriorResponse() {
}

public InteriorResponse(int startWeek, int endWeek, String pno12) {
    super();
    this.sWeek = startWeek;
    this.eWeek = endWeek;
    this.p12 = pno12;
}

public int getStartWeek() {
    return sWeek;
}
public void setStartWeek(int startWeek) {
    this.sWeek = startWeek;
}
public int getEndWeek() {
    return eWeek;
}
public void setEndWeek(int endWeek) {
    this.eWeek = endWeek;
}
public String getPno12() {
    return p12;
}
public void setPno12(String pno12) {
    this.p12 = pno12;
}
public List<Feature> getFeatureList() {
    return featureList;
}

@XmlElement(name = "FeatureList")
public void setFeatureList(List<Feature> featureList) {
    this.featureList = featureList;
}           
 }

另一个 Java 功能:-

@XmlRootElement(name = "Feature")
 public class Feature {
//@XmlElement(name = "Feature")
private String feature_;
@XmlElement(name = "code")
private String code_;

public String getCode() {
    return code_;
}

public void setCode(String code) {
    this.code_ = code;
}

public String getFeature_() {
    return feature_;
}

public void setFeature_(String feature_) {
    this.feature_ = feature_;
}
 }

我将上面的类用作:-

    public static void xmlToInterior() {
    File file = new File("minxml.xml");
    JAXBContext jaxbContext;

    try {
        jaxbContext = JAXBContext.newInstance(InteriorResponse.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        InteriorResponse interiorFeatures = (InteriorResponse) unmarshaller.unmarshal(file);
        List<Feature> list_feat =  interiorFeatures.getFeatureList();
        for(Feature ft : list_feat) {
            System.out.println(ft.getCode());
        }
    } catch (JAXBException e) {
        e.printStackTrace();
    }       
 }

我的输出为:- list_feat
code_null
feature_null

而 list_feat 的大小是 1。它应该是 2。

我还必须命名类成员 sWeek 而不是 startWeek。否则, jaxbContext = JAXBContext.newInstance(InteriorResponse.class) 会抛出异常,例如存在同名的 2 个元素。

XML 附加部分

我认为我可以完成 XML 的扩孔部分。我正在一步一步地尝试。但我做不到。我真的需要找到一些关于 JXB 的好教程或书籍。我只找到了 JXB 的简短概述。关于在哪里详细阅读的任何建议?

<Features_res>
<StartWeek>202017</StartWeek>
<EndWeek>202035</EndWeek>
<Pno12>ABCDEEB</Pno12>

<FeatureList>
    <Feature>
        <Code>T002</Code>
    </Feature>
    <Feature>
        <Code>T002</Code>
    </Feature>
</FeatureList>

 <OptionList>
    <Option>001048</Option>
    <Option>000050</Option>
    <Option>000790</Option>
 </OptionList>  
</Features_res>

所以我将新课程设置为:-

    public class OptionList {

    private List<Option> options;

    @XmlElement(name = "Option")
    public List<Option> getOptions() {
        return options;
    }

    public void setOptions(List<Option> options) {
        this.options = options;
    }
}

另一个类:-

    import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

public class Option {

    @XmlElement(name = "Option")
    private String option_;

    public String getOption() {
        return option_;
    }


    public void setOption(String option) {
        this.option_ = option;
    }   
}

InteriorResponse 类更新为:-

    import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Features_res")
public class InteriorResponse {

  @XmlElement(name = "StartWeek")
  private int sWeek;

  @XmlElement(name = "EndWeek")
  private int eWeek;

  @XmlElement(name = "Pno12")
  private String p12;

  private FeatureList featureList;

  private OptionList optionList;

  public InteriorResponse() {}

  public InteriorResponse(int startWeek, int endWeek, String pno12) {
    super();
    this.sWeek = startWeek;
    this.eWeek = endWeek;
    this.p12 = pno12;
  }

  public int getStartWeek() {
    return sWeek;
  }

  public void setStartWeek(int startWeek) {
    this.sWeek = startWeek;
  }

  public int getEndWeek() {
    return eWeek;
  }

  public void setEndWeek(int endWeek) {
    this.eWeek = endWeek;
  }

  public String getPno12() {
    return p12;
  }

  public void setPno12(String pno12) {
    this.p12 = pno12;
  }

  @XmlElement(name = "FeatureList")
  public FeatureList getFeatureList() {
    return featureList;
  }

  public void setFeatureList(FeatureList featureList) {
    this.featureList = featureList;
  }

  @XmlElement(name = "OptionList")
  public OptionList getOptionList() {
    return optionList;
}

public void setOptionList(OptionList optionList) {
    this.optionList = optionList;
}

@Override
  public String toString() {
    return "InteriorResponse{"
        + "sWeek="
        + sWeek
        + ", eWeek="
        + eWeek
        + ", p12='"
        + p12
        + '\''
        + ", featureList="
        + featureList
        + '}';
  }
}

我无法获得选项。 选项空

整个 xml 真的很大。我仍然想自己尝试剩下的部分。

【问题讨论】:

  • 你能把完整的xml内容贴在这里吗?
  • 您提供的Xml内容格式不正确,您可以检查一下。
  • 对不起,现在已经满了。我错过了最后一个结束标签
  • 我保存后它没有添加。但在预览中它对我来说很好。让我再试一次
  • 现在好了。请看一下

标签: java xml jaxb


【解决方案1】:

你需要根据 XML 结构来设计你的类。在下面找到类。

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Features_res")
public class InteriorResponse {

  @XmlElement(name = "StartWeek")
  private int sWeek;

  @XmlElement(name = "EndWeek")
  private int eWeek;

  @XmlElement(name = "Pno12")
  private String p12;

  private FeatureList featureList;

  private OptionList optionList;

  public InteriorResponse() {}

  public InteriorResponse(int startWeek, int endWeek, String pno12) {
    super();
    this.sWeek = startWeek;
    this.eWeek = endWeek;
    this.p12 = pno12;
  }

  public int getStartWeek() {
    return sWeek;
  }

  public void setStartWeek(int startWeek) {
    this.sWeek = startWeek;
  }

  public int getEndWeek() {
    return eWeek;
  }

  public void setEndWeek(int endWeek) {
    this.eWeek = endWeek;
  }

  public String getPno12() {
    return p12;
  }

  public void setPno12(String pno12) {
    this.p12 = pno12;
  }

  @XmlElement(name = "FeatureList")
  public FeatureList getFeatureList() {
    return featureList;
  }

  public void setFeatureList(FeatureList featureList) {
    this.featureList = featureList;
  }

  @XmlElement(name = "OptionList")
  public OptionList getOptionList() {
    return optionList;
  }

  public void setOptionList(OptionList optionList) {
    this.optionList = optionList;
  }

  @Override
  public String toString() {
    return "InteriorResponse{"
        + "sWeek="
        + sWeek
        + ", eWeek="
        + eWeek
        + ", p12='"
        + p12
        + '\''
        + ", featureList="
        + featureList
        + ", optionList="
        + optionList
        + '}';
  }
}

特征对象的类

import javax.xml.bind.annotation.XmlElement;

public class Feature {

  @XmlElement(name = "Code")
  private String code_;

  public String getCode() {
    return code_;
  }

  public void setCode(String code) {
    this.code_ = code;
  }

  @Override
  public String toString() {
    return "Feature{" + "code_='" + code_ + '\'' + '}';
  }
}

FeatureList 类

import javax.xml.bind.annotation.XmlElement;
import java.util.List;

public class FeatureList {

  private List<Feature> features;

  @XmlElement(name = "Feature")
  public List<Feature> getFeatures() {
    return features;
  }

  public void setFeatures(List<Feature> features) {
    this.features = features;
  }
}

OptionList 类

import javax.xml.bind.annotation.XmlElement;
import java.util.List;

public class OptionList {
  private List<String> option;

  @XmlElement(name = "Option")
  public List<String> getOption() {
    return option;
  }

  public void setOption(List<String> option) {
    this.option = option;
  }
}

为了测试和简单,我在下面写了一个小的java测试程序。

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;

public class Test {

  public static void main(String[] args) {
    File file =
        new File("E:\\so\\xml\\minxml.xml");
    JAXBContext jaxbContext;

    try {
  jaxbContext = JAXBContext.newInstance(InteriorResponse.class);
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  InteriorResponse interiorFeatures = (InteriorResponse) unmarshaller.unmarshal(file);

  List<Feature> list_feat = interiorFeatures.getFeatureList().getFeatures();

  List<String> optionList = interiorFeatures.getOptionList().getOption();
  for (String option : optionList) {
    System.out.println("Option Value : " + option);
  }

  for (Feature ft : list_feat) {
    System.out.println(ft.getCode());
  }
} catch (JAXBException e) {
  e.printStackTrace();
}
  }
}

我还提供了下面的示例 xml 结构。

    <Features_res>
    <StartWeek>202017</StartWeek>
    <EndWeek>202035</EndWeek>
    <Pno12>ABCDEEB</Pno12>

    <FeatureList>
        <Feature>
            <Code>T002</Code>
        </Feature>
        <Feature>
            <Code>T002</Code>
        </Feature>
    </FeatureList>

    <OptionList>
        <Option>001048</Option>
        <Option>000050</Option>
        <Option>000790</Option>
    </OptionList>
</Features_res>

【讨论】:

  • 完美运行。请问您为什么不知道为什么我的变量名不能与 XML 的标签名称相同?
  • 能否请您检查更新后的问题?
  • 如果我理解您的问题,变量名称可以是任何名称,但您必须在 XMlElement 注释中提供 Xml 标记的名称。
  • 请检查更新问题中是否有额外部分:“ OptionList> " 为此,我按照您的方法制作了 2 个新类 OptionList 和 Option。但是我的代码中的选项为 null 。一定有什么问题。额外部分与“T001 T002 ”不同是2级,但 1 级。由于缺少 jaxb,我不能
  • 您也可以尝试将 OptionList 的 FeatureList 类型对象作为包含另一个对象作为选项的对象。
【解决方案2】:

我对 JAXB 的东西知之甚少。我从 Sambit 那里学到了很多东西,他提供了很多帮助,并给了我开始使用这个 JAXB 的方法。后来我用更少的 Java 类和更智能的 JAXB 注释实现了我的版本。

我的内部响应类版本:-

    import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name = "Features_res")
public class InteriorResponse {

    @XmlElement(name = "StartWeek")
    private int startWeek;

    @XmlElement(name = "EndWeek")
    private int endWeek;

    @XmlElement(name = "Pno12")
    private String pno12;

    @XmlElementWrapper(name = "FeatureList")
    @XmlElement(name = "Feature")
    private List<Feature> featureList;

    @XmlElementWrapper(name = "OptionList")
    @XmlElement(name = "Option")
    private List<String> optionList;

    public InteriorResponse() {
    }

    public InteriorResponse(int startWeek, int endWeek, String pno12) {
        super();
        this.startWeek = startWeek;
        this.endWeek = endWeek;
        this.pno12 = pno12;
    }

    @XmlTransient
    public int getStartWeek() {
        return startWeek;
    }

    public void setStartWeek(int startWeek) {
        this.startWeek = startWeek;
    }

    @XmlTransient
    public int getEndWeek() {
        return endWeek;
    }

    public void setEndWeek(int endWeek) {
        this.endWeek = endWeek;
    }

    @XmlTransient
    public String getPno12() {
        return pno12;
    }

    public void setPno12(String pno12) {
        this.pno12 = pno12;
    }

    @XmlTransient
    public List<Feature> getFeatureList() {
        return featureList;
    }

    public void setFeatureList(List<Feature> featureList) {
        this.featureList = featureList;
    }

    @XmlTransient
    public List<String> getOptionList() {
        return optionList;
    }

    public void setOptionList(List<String> optionList) {
        this.optionList = optionList;
    }

    @Override
    public String toString() {
        return "InteriorResponse{" + "sWeek=" + startWeek + ", eWeek=" + endWeek + ", pno12='" + pno12 + '\'' + ", featureList=" + featureList + ", optionList="
            + optionList + '}';
    }
}

我的功能类版本:-

    import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

public class Feature {

    @XmlElement(name = "Code")
    private String code;

    @XmlTransient
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Feature{" + "code_='" + code + '\'' + '}';
    }
}

请注意,FeaturerList 和 OptionList 不需要任何额外的包装类。它可以通过 JAXB 注释 @XmlElementWrapper(name = "FeatureList") 来完成。另外,学到了一个非常重要的教训。我们必须将所有属性的getter 方法标记为@XmlTransient。否则,JAXB 会抛出异常 2 发现具有相同名称的属性。因为我们的类的所有属性对 JAXB 都是可见的。所以我们必须将一个标记为@XmlTransient。

在我看来,这是一个比公认答案更好的解决方案。我把所有的功劳都归功于 Sambit。我希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2022-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    相关资源
    最近更新 更多