簡體   English   中英

為什么JAXB java xml unmarshaller返回null

[英]why JAXB java xml unmarshaller return null

我正在嘗試將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 InteriorResponse:-

@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)會引發異常,例如存在兩個具有相同名稱的元素。

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結構設計您的類。 在課程下方找到。

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
        + '}';
  }
}

Feature對象的類

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>

我對JAXB的知識知之甚少。 我從Sambit那里學到了很多東西,他提供了很多幫助,並為我提供了開始使用此JAXB的方法。 后來,我用更少的Java類和更聰明地使用JAXB注釋實現了我的版本。

我的InteriorResponse類:

    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 + '\'' + '}';
    }
}

請注意,對於FeatuerList和OptionList,我不需要任何額外的包裝器類。 可以通過JAXB批注@XmlElementWrapper(name =“ FeatureList”)來完成。 此外,還吸取了非常重要的教訓。 我們必須將所有屬性的getter方法標記為@XmlTransient。 否則,JAXB會拋出一個異常2,它們具有相同的名稱。 因為我們的類的所有屬性對於JAXB都是可見的。 因此,我們必須將其標記為@XmlTransient。

在我看來,這是比公認的答案更好的解決方案。 我把一切歸功於Sambit。 我認為這會對他人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM