[英]Issue with STAX parsing in Java
我有一個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)包含主類
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)eenum文件
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
任何人都可以指出這些值在重復的問題。
麻煩的是您為dist
元素創建Employee記錄。
if ("dist".equals(reader.getLocalName())){
currEmp = new Employee();
currEmp.id = reader.getAttributeValue(null, "id");
}
這是我的多個員工定義。 有效地將最后一個元素數據寫入Employee記錄。
我會將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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.