Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从XML文档中按顺序获取键和值_Java_Xml_Parsing_Xpath - Fatal编程技术网

Java 如何从XML文档中按顺序获取键和值

Java 如何从XML文档中按顺序获取键和值,java,xml,parsing,xpath,Java,Xml,Parsing,Xpath,在这里,我解析一个给定的XML文档并获取元素节点键和值。然而,我得到这些值的顺序是随机的。我怎样才能使它从文档的顶部到底部按顺序排列 示例:[MonthlyPlanPremiumAmtPP、MonthlyAdvancedPTCAmtPP、MonthCdPP等] 是因为我的数据结构吗?我正在从地图转换到列表 XML文档: <Return xmlns="http://www.irs.gov/efile"> <ReturnData> <IRS1095A uui

在这里,我解析一个给定的XML文档并获取元素节点键和值。然而,我得到这些值的顺序是随机的。我怎样才能使它从文档的顶部到底部按顺序排列

示例:[MonthlyPlanPremiumAmtPP、MonthlyAdvancedPTCAmtPP、MonthCdPP等]

是因为我的数据结构吗?我正在从地图转换到列表

XML文档:

<Return xmlns="http://www.irs.gov/efile">
  <ReturnData>
    <IRS1095A uuid="a77f40a2-af31-4404-a27d-4c1eaad730c2">
      <MonthlyPTCInformationGrpPP uuid="69dc9dd5-5415-4ee4-a199-19b2dbb701be">
        <MonthlyPlanPremiumAmtPP>136</MonthlyPlanPremiumAmtPP>
        <MonthlyAdvancedPTCAmtPP>125</MonthlyAdvancedPTCAmtPP>
        <MonthCdPP>SEPTEMBER</MonthCdPP>
        <MonthlyPremiumSLCSPAmtPP>250</MonthlyPremiumSLCSPAmtPP>
      </MonthlyPTCInformationGrpPP>
    </IRS1095A>
    <IRS1040>
      <IndividualReturnFilingStatusCd>1</IndividualReturnFilingStatusCd>
      <WagesSalariesAndTipsAmt>22000</WagesSalariesAndTipsAmt>
      <TotalExemptionsCnt>1</TotalExemptionsCnt>
      <AdjustedGrossIncomeAmt>22000</AdjustedGrossIncomeAmt>
    </IRS1040>
  </ReturnData>
  <ReturnHeader>
    <SelfSelectPINGrp>
      <PrimaryBirthDt>1970-01-01</PrimaryBirthDt>
    </SelfSelectPINGrp>
    <Filer>
      <PrimarySSN>555-11-2222</PrimarySSN>
      <PrimaryResidentStatesInfoGrpPP>
        <ResidentStateInfoPP uuid="a77f40a2-af31-4404-a27d-4c1eaad730c2">
          <ResidentStateAbbreviationCdPP>CA</ResidentStateAbbreviationCdPP>
        </ResidentStateInfoPP>
      </PrimaryResidentStatesInfoGrpPP>
    </Filer>
  </ReturnHeader>
</Return>

136
125
九月
250
1.
22000
1.
22000
1970-01-01
555-11-2222
加利福尼亚州
代码:

@测试
public void testGetXMLModelData()引发异常{
文件f=新文件(“xmlDir/example.xml”);
模型m=getXMLModelData(f);
debug(“Models键:”+m.getInputs());
debug(“模型值:”+m.getValues());
}
公共映射p(文件)引发异常{
Map Map=newhashmap();
XMLStreamReader xr=XMLInputFactory.newInstance().createXMLStreamReader(新文件InputStream(文件));
而(xr.hasNext()){
int e=xr.next();
if(e==XMLStreamReader.START_元素){
//进一步实施
} 
}
返回图;
}
公共模型getXMLModelData(文件f)引发异常{
模型=新模型();
Map=p(f);
List listKeys=newArrayList(map.keySet());
listListValues=newArrayList(map.values());
model.setInputs(列表键);
model.setValues(listValues);
收益模型;
}
以下是我定义为模型的类:

public class Model {

    public static final int INPUTS_ROW = 5;

    private String topic;
    private List<String> inputs;
    private List<String> values;

    public Model() {
        inputs = new ArrayList<String>();
    }


    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public List<String> getInputs() {
        return inputs;
    }

    public void setInputs(List<String> inputs) {
        this.inputs = inputs;
    }

    public List<String> getValues() {
        return values;
    }

    public void setValues(List<String> values) {
        this.values = values;
    }

}
公共类模型{
公共静态最终整数输入_行=5;
私有字符串主题;
私人清单投入;
私有列表值;
公共模型(){
输入=新的ArrayList();
}
公共字符串getTopic(){
返回主题;
}
公共void集合主题(字符串主题){
this.topic=主题;
}
公共列表getInputs(){
返回输入;
}
公共输入(列表输入){
这个输入=输入;
}
公共列表getValues(){
返回值;
}
公共void设置值(列表值){
这个值=值;
}
}

问题在于HashMap,因为该数据结构以无序方式列出。解决方案是使用
LinkedHashMap
,它将保留插入地图的顺序,从上到下扫描文档

问题在于HashMap,因为该数据结构以无序方式列出。解决方案是使用
LinkedHashMap
,它将保留插入地图的顺序,从上到下扫描文档

public class Model {

    public static final int INPUTS_ROW = 5;

    private String topic;
    private List<String> inputs;
    private List<String> values;

    public Model() {
        inputs = new ArrayList<String>();
    }


    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public List<String> getInputs() {
        return inputs;
    }

    public void setInputs(List<String> inputs) {
        this.inputs = inputs;
    }

    public List<String> getValues() {
        return values;
    }

    public void setValues(List<String> values) {
        this.values = values;
    }

}