【问题标题】:Issue with STAX parsing in JavaJava中的STAX解析问题
【发布时间】:2013-08-06 16:14:13
【问题描述】:

我有一个如下的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<top>
<dist id="1">
<emp name="gaara" age="22" location="konoha"/>
<emp name="vegeta" age="44" location="namek"/>
<assitant name="nappa" age="64" location="namek"/>
</dist>

<dist id="2">
<emp name="naruto" age="20" location="konoha"/>
<emp name="lei" age="21" location="sand"/>
<assitant name="gohan" age="25" location="island"/>
</dist>

</top>

预期结果如下:

Required O/P
1 | gaara | 22 | konoha
1 | vegeta | 44 | namek
2 | naruto | 20 | konoha
2 | lei | 21 |sand

谁能解释我该怎么做。一件事是输出不应包含辅助元素数据。

我会发布代码,但由于元素多次出现,我的代码无法正常工作。

编辑 我正在发布代码:

1) 包含main的类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;


public class loadNwccData {
  public static void main(String[] args) throws XMLStreamException, FileNotFoundException {
    List<Employee> empList = null;
    Employee currEmp = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();


    InputStream inputStream= new FileInputStream("C:\\Documents and Settings\\samp\\Desktop\\sample.xml");

    XMLStreamReader reader = 
        factory.createXMLStreamReader(inputStream);

    while(reader.hasNext()){
      int event = reader.next();

      switch(event){
        case XMLStreamConstants.START_ELEMENT: 
          if ("dist".equals(reader.getLocalName())){
            currEmp = new Employee();
            currEmp.id = reader.getAttributeValue(null, "id");
          }
          else if ("emp".equals(reader.getLocalName())){

              currEmp.name = reader.getAttributeValue(null, "name");
              currEmp.age = reader.getAttributeValue(null, "age");
              currEmp.location = reader.getAttributeValue(null, "location");
            }
          if("top".equals(reader.getLocalName())){
            empList = new ArrayList<Employee>();
          }
          break;


            case XMLStreamConstants.END_ELEMENT:
            Cloumns value = Cloumns.valueOf(reader.getLocalName());
          switch(value){
            case emp:
              empList.add(currEmp);
              break;

          }
          break;

        case XMLStreamConstants.START_DOCUMENT:
          empList = new ArrayList<Employee>();
          break;
      }

    }

    //Print the employee list populated from XML
    for ( Employee emp : empList){
      System.out.println(emp);
    }

  }
}

class Employee{
  String id;
  String name;
  String age;
  String location;

  @Override
  public String toString(){
    return id +" | " +name +" | "+age +" | "+location;
  }
}

2) 枚举文件

public enum Cloumns {emp,assitant,dist,top};

3) 我得到的 O/P:

1 | vegeta | 44 | namek
1 | vegeta | 44 | namek
2 | lei | 21 | sand
2 | lei | 21 | sand

由于值重复,任何人都可以指出这里的问题。

【问题讨论】:

  • 张贴代码;这将有助于获得答案
  • @Jayan :发布代码和我得到的 O/P。

标签: java stax


【解决方案1】:

问题是您为dist 元素创建了员工记录。

if ("dist".equals(reader.getLocalName())){
            currEmp = new Employee();
            currEmp.id = reader.getAttributeValue(null, "id");
          }

这遵循了我的多员工定义。有效地将最后一个元素数据放入员工记录中。

我会将 id 存储到一个变量中,例如 currentDistID 并将其与 Employee 一起使用。应为每个 employee 元素创建员工记录。

代码

  public static void main(String[] args) throws XMLStreamException, FileNotFoundException {
    List<Employee> empList = null;
    Employee currEmp = null;

    XMLInputFactory factory = XMLInputFactory.newInstance();
    String currentDistID=null;

    InputStream inputStream= new FileInputStream("c:\\sample.xml");

    XMLStreamReader reader =
            factory.createXMLStreamReader(inputStream);

    while(reader.hasNext()){
        int event = reader.next();

        switch(event){
            case XMLStreamConstants.START_ELEMENT:
                if ("dist".equals(reader.getLocalName())){
                    //all subsequent employees share this.
                    currentDistID = reader.getAttributeValue(null, "id");
                }
                else if ("emp".equals(reader.getLocalName())){
                    currEmp = new Employee();
                    currEmp.id=currentDistID;
                    currEmp.name = reader.getAttributeValue(null, "name");
                    currEmp.age = reader.getAttributeValue(null, "age");
                    currEmp.location = reader.getAttributeValue(null, "location");
                }
                if("top".equals(reader.getLocalName())){
                    empList = new ArrayList<Employee>();
                }
                break;


            case XMLStreamConstants.END_ELEMENT:
                Cloumns value = Cloumns.valueOf(reader.getLocalName());
                switch(value){
                    case emp:
                        empList.add(currEmp);
                        break;

                }
                break;

            case XMLStreamConstants.START_DOCUMENT:
                //empList = new ArrayList<Employee>();
                break;
        }

    }

    //Print the employee list populated from XML
    for ( Employee emp : empList){
        System.out.println(emp);
    }

}

【讨论】:

  • 非常感谢,问题已解决,感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 2011-07-21
  • 2010-11-05
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多