Clarification in Java STAX Parsing
I have an xml file which I'm reading using STAX.
The XML file is as shown:
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="111" idd="121">
<firstName>Rakesh</firstName>
<lastName>Mishra</lastName>
<location>Bangalore</location>
</employee>
<employee id="112" idd="122">
<firstName>John</firstName>
<lastName>Davis</lastName>
<location>Chennai</location>
</employee>
<employee id="113" idd="123">
<firstName>Rajesh</firstName>
<lastName>Sharma</lastName>
<location>Pune</location>
</employee>
</employees>
I have one class reading the file and one enum for string switching.
The Java Class is:
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;
String tagContent = null;
XMLInputFactory factory = XMLInputFactory.newInstance();
File file = new File("c:\\file-utf.xml");
InputStream inputStream= new FileInputStream("C:\\Documents and
Settings\\samp\\Desktop\\emp.xml");
XMLStreamReader reader =
factory.createXMLStreamReader(inputStream);
while(reader.hasNext()){
int event = reader.next();
switch(event){
case XMLStreamConstants.START_ELEMENT:
if ("employee".equals(reader.getLocalName())){
currEmp = new Employee();
currEmp.id = reader.getAttributeValue(null, "idd");
}
if("employees".equals(reader.getLocalName())){
empList = new ArrayList<Employee>();
}
break;
case XMLStreamConstants.CHARACTERS:
tagContent = reader.getText().trim();
break;
case XMLStreamConstants.END_ELEMENT:
Cloumns value = Cloumns.valueOf(reader.getLocalName());
switch(value){
case employee:
empList.add(currEmp);
break;
case firstName:
currEmp.firstName = tagContent;
break;
case lastName:
currEmp.lastName = tagContent;
break;
case location:
currEmp.location = tagContent;
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 firstName;
String lastName;
String location;
@Override
public String toString(){
return firstName+" "+lastName+"("+id+") "+location;
}
}
The enum is:
public enum Cloumns {employees , employee,firstName, lastName ,location};
Now how do I modify the class if I have to read the following XML file
with the format shown below.
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="111" idd="121">
<firstName id="1">Rakesh</firstName>
<firstName id="2">Rakesh2</firstName>
<lastName>Mishra</lastName>
<location>Bangalore</location>
</employee>
<employee id="112" idd="122">
<firstName id="1">John</firstName>
<firstName id="2">John2</firstName>
<lastName>Davis</lastName>
<location>Chennai</location>
</employee>
<employee id="113" idd="123">
<firstName id="1">Rajesh</firstName>
<firstName id="2">Rajesh2</firstName>
<lastName>Sharma</lastName>
<location>Pune</location>
</employee>
</employees>
And the desired output is:
Employee id=111 idd=121
firstname id=1 Rakesh
firstname id=2 Rakesh2
lastname Mishra
location Bangalore
Employee id=112 idd=122
firstname id=1 John
firstname id=2 John2
lastname Davis
location Chennai
....
No comments:
Post a Comment