PersonHandler.java
package com.example.xml;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class PersonHandler extends DefaultHandler {
private List<Person> list;
private Person person;
private StringBuilder sb;
public PersonHandler() {
list = new ArrayList<>();
sb = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("person")) {
person = new Person();
int id = Integer.parseInt(attributes.getValue("id"));
person.setId(id);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("name")) {
person.setName(sb.toString().trim());
sb.setLength(0);
} else if (qName.equals("company")) {
person.setCompany(sb.toString().trim());
sb.setLength(0);
} else if (qName.equals("person")) {
list.add(person);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
sb.append(ch, start, length);
}
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
}
SAXParserTest.java
package com.example.xml;
import java.io.File;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class SAXParserTest {
public static void main(String[] args) {
String path = SAXParserTest.class.getResource("").getPath();
System.out.println(path);
File file = new File(path, "personList.xml");
System.out.println(file.getPath());
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
PersonHandler handler = new PersonHandler();
try {
parser = factory.newSAXParser();
parser.parse(file, handler);
} catch (Exception e) {
e.printStackTrace();
}
List<Person> list = handler.getList();
for (Person person : list) {
System.out.println(person);
}
}
}
personList.xml
<?xml version="1.0" encoding="UTF-8"?>
<list>
<person id="1">
<name>홍길동</name>
<company>u-angel co.</company>
</person>
<person id="2">
<name>성춘향</name>
<company><![CDATA[던킨 & 도너츠]]></company>
</person>
</list>
Person.java
package com.example.xml;
public class Person {
private int id;
private String name;
private String company;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", company=" + company + "]";
}
}
'IT > Java' 카테고리의 다른 글
java - console에 DB데이터 출력(행갯수, 열갯수, 열제목, 데이터), ResultSetMetaData (0) | 2018.11.21 |
---|---|
java - url을 통해 가져온 데이터를 DOMParser로 파싱, JSONParser로 파싱 (0) | 2018.11.21 |
java - url을 통해 가져온 xml을 DOM 파서로 NodeList 출력 (0) | 2018.11.20 |
java - xml DOM파서 (0) | 2018.11.20 |
java - 이메일 보내기 자바코드, GUI 이메일 보내기 코드 (JFrame), 웹페이지에서 이메일 보내기 (0) | 2018.11.19 |