Read XML file from Java
1. Get XML file ready like below
person.xml
<?xml version = "1.0" encoding = "utf-8"?>
<persons>
<person id="15">
<firstName>Piyush</firstName>
<lastName>Trivedi</lastName>
<age>15</age>
<email>piyush@gmail.com</email>
</person>
<person id="16">
<firstName>Vibha</firstName>
<lastName>Singh</lastName>
<age>10</age>
<email>vibha@gmail.com</email>
</person>
</persons>
2. Create a Person class
public class Person {
private int id;
private String firstName;
private String lastName;
private int age;
private String email;
//setters.......getters......
}
3. Create Handler class by extending DefaultHandler
public class PersonHandler extends DefaultHandler {
private List<Person> persons = new ArrayList<>();
private Person person;
private String content;
/*
* Receive notification of the start of an element.
*
* @see org.xml.sax.helpers.DefaultHandler#startElement(String, String,
* String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
if ("person".equalsIgnoreCase(qName)) {
person = new Person();
String id = attrs.getValue("id");
person.setId(Integer.parseInt(id));
}
}
/**
* Receive notification of character data inside an element.
*/
public void characters(char chars[], int start, int length) throws SAXException {
content = new String(chars, start, length);
}
/**
* Receive notification of the end of an element.
*/
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("person".equalsIgnoreCase(qName)) {
getPersons().add(person);
}
switch (qName) {
case "firstName":
person.setFirstName(content);
break;
case "lastName":
person.setLastName(content);
break;
case "age":
person.setAge(Integer.parseInt(content));
break;
case "email":
person.setEmail(content);
break;
}
}
public List<Person> getPersons() {
return persons;
}
}
4. read XML file from some other class
static List<Person> readFromXMLFile() {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser;
PersonHandler handler = new PersonHandler();
try {
parser = spf.newSAXParser();
parser.parse("persons.xml", handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return handler.getPersons();
}
person.xml
<?xml version = "1.0" encoding = "utf-8"?>
<persons>
<person id="15">
<firstName>Piyush</firstName>
<lastName>Trivedi</lastName>
<age>15</age>
<email>piyush@gmail.com</email>
</person>
<person id="16">
<firstName>Vibha</firstName>
<lastName>Singh</lastName>
<age>10</age>
<email>vibha@gmail.com</email>
</person>
</persons>
2. Create a Person class
public class Person {
private int id;
private String firstName;
private String lastName;
private int age;
private String email;
//setters.......getters......
}
3. Create Handler class by extending DefaultHandler
public class PersonHandler extends DefaultHandler {
private List<Person> persons = new ArrayList<>();
private Person person;
private String content;
/*
* Receive notification of the start of an element.
*
* @see org.xml.sax.helpers.DefaultHandler#startElement(String, String,
* String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
if ("person".equalsIgnoreCase(qName)) {
person = new Person();
String id = attrs.getValue("id");
person.setId(Integer.parseInt(id));
}
}
/**
* Receive notification of character data inside an element.
*/
public void characters(char chars[], int start, int length) throws SAXException {
content = new String(chars, start, length);
}
/**
* Receive notification of the end of an element.
*/
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("person".equalsIgnoreCase(qName)) {
getPersons().add(person);
}
switch (qName) {
case "firstName":
person.setFirstName(content);
break;
case "lastName":
person.setLastName(content);
break;
case "age":
person.setAge(Integer.parseInt(content));
break;
case "email":
person.setEmail(content);
break;
}
}
public List<Person> getPersons() {
return persons;
}
}
4. read XML file from some other class
static List<Person> readFromXMLFile() {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser;
PersonHandler handler = new PersonHandler();
try {
parser = spf.newSAXParser();
parser.parse("persons.xml", handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return handler.getPersons();
}
Comments
Post a Comment