IT/Java

java - xml SAXParser로 파싱하기

노마드오브 2018. 11. 21. 18:31

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 + "]";

}

}