IT/Java

java - xml DOM파서

노마드오브 2018. 11. 20. 18:13

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

}

}




DOMParserTest.java

package com.example.xml;


import java.io.File;

import java.util.ArrayList;

import java.util.List;


import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;


public class DOMParserTest {


public static void main(String[] args) {

        // 자바에서 xml 파서 2가지

        // 1) DOMParser 돔파서

        // 2) SAXParser 삭스파서

// xml 파일의 경로

// String path = "D:\\docs\\workspace_web\\javaproject\\src\\com\\example\\xml\\personList.xml";

String path = DOMParserTest.class.getResource(".").getPath();

System.out.println(path);

// 읽어들일 파일 정보 준비

File file = new File(path, "personList.xml");

System.out.println(file.getPath());

List<Person> list = new ArrayList<>();

// DOM 파서 준비

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = null;

Document document = null;

try {

builder = factory.newDocumentBuilder();

document = builder.parse(file);

} catch (Exception e) {

e.printStackTrace();

}

NodeList personNodeList = document.getElementsByTagName("person");

// getElementsByTagName() 메소드는 null을 리턴하지 않음

for (int i=0; i<personNodeList.getLength(); i++) {

Person person = new Person();

Node personNode = personNodeList.item(i);

// person 요소의 속성 가져오기(속성=Map)

NamedNodeMap attrMap = personNode.getAttributes();

for (int j=0; j<attrMap.getLength(); j++) {

Node attrNode = attrMap.item(j);

if (attrNode.getNodeName().equals("id")) {

String id = attrNode.getNodeValue();

person.setId(Integer.parseInt(id));

} else if (attrNode.getNodeName().equals("addr")) {

                    

                }

} // for 열 중에서 속성값

NodeList childNodeList = personNode.getChildNodes();

for (int j=0; j<childNodeList.getLength(); j++) {

Node childNode = childNodeList.item(j);

if (childNode.getNodeName().equals("name")) {

// String name = childNode.getChildNodes().item(0).getNodeValue();

String name = childNode.getFirstChild().getNodeValue();

person.setName(name);

} else if (childNode.getNodeName().equals("company")) {

String company = childNode.getFirstChild().getNodeValue();

person.setCompany(company);

}

} // for 열 중에서 필드값

list.add(person);

} // for 행


System.out.println();

for (Person p : list) {

System.out.println(p);

}

System.out.println();

} // main


}