Java中的STAX解析问题
我有一个XML文件,如下所示:Java中的STAX解析问题,java,stax,Java,Stax,我有一个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"
<?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;
}
}
3) 我得到的O/p:
1 | vegeta | 44 | namek
1 | vegeta | 44 | namek
2 | lei | 21 | sand
2 | lei | 21 | sand
任何人都可以指出这里的问题是什么,因为这些值在重复。问题在于您为
dist
元素创建员工记录
if ("dist".equals(reader.getLocalName())){
currEmp = new Employee();
currEmp.id = reader.getAttributeValue(null, "id");
}
这是我的多员工定义。有效地将最后一个元素数据写入员工记录
我会将id存储到一个变量中,比如说currentDistID
,并将其用于Employee。应为每个员工
元素创建员工记录
代码
publicstaticvoidmain(字符串[]args)抛出XMLStreamException、FileNotFoundException{
List empList=null;
员工currenp=null;
XMLInputFactory=XMLInputFactory.newInstance();
字符串currentDistID=null;
InputStream InputStream=新文件InputStream(“c:\\sample.xml”);
XMLStreamReader阅读器=
createXMLStreamReader(inputStream);
while(reader.hasNext()){
int event=reader.next();
开关(事件){
案例XMLStreamConstants.START_元素:
if(“dist”.equals(reader.getLocalName())){
//所有后续员工都共享此信息。
currentDistID=reader.getAttributeValue(null,“id”);
}
else if(“emp.equals(reader.getLocalName())){
currenp=新员工();
currenp.id=currentDistID;
currenp.name=reader.getAttributeValue(null,“name”);
currenp.age=reader.getAttributeValue(null,“age”);
currenp.location=reader.getAttributeValue(null,“位置”);
}
if(“top”.equals(reader.getLocalName())){
empList=newarraylist();
}
打破
案例XMLStreamConstants.END_元素:
Cloumns value=Cloumns.valueOf(reader.getLocalName());
开关(值){
案例emp:
雇主添加(currenp);
打破
}
打破
案例XMLStreamConstants.START_文档:
//empList=newarraylist();
打破
}
}
//打印从XML填充的员工列表
for(员工emp:员工列表){
系统输出打印项次(emp);
}
}
请务必张贴代码;这将有助于answers@Jayan:发布了代码和我收到的订单。非常感谢,问题已经解决,感谢您的解释。
if ("dist".equals(reader.getLocalName())){
currEmp = new Employee();
currEmp.id = reader.getAttributeValue(null, "id");
}
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);
}
}