jaxb

java로 xml 파싱하기 jaxb

아래의 xml을 파싱하는 법이 여러가지 있는데 여기서는 jaxb로 파싱하는 법을 작성하려고 한다.

1
2
3
4
5
6
7
8
9
10
11
12

<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

그럼 먼저 라이브러리 추가

1
2
3
4
5
6
7

<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>

여기서 파싱할 객체를 만들어 보면

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

package com.github.sejoung.jaxb.model;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@XmlRootElement(name = "GlobalNamingResources")
public class GlobalNamingResources {

@XmlElement(name = "Resource")
private final List<Resource> resource = new ArrayList<>();


}


위에 객체를 만들고

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

package com.github.sejoung.jaxb.model;


import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@XmlRootElement(name = "Resource")
public class Resource {

@XmlAttribute
private String name;
@XmlAttribute
private String auth;
@XmlAttribute
private String type;
@XmlAttribute
private String description;
@XmlAttribute
private String factory;
@XmlAttribute
private String pathname;


}


또 하나더 만든다음

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

package com.github.sejoung.jaxb;

import com.github.sejoung.jaxb.model.GlobalNamingResources;
import com.github.sejoung.jaxb.model.Resource;
import java.io.FileNotFoundException;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class JaxbMain {

public static void main(String... args) throws JAXBException {

JAXBContext context = JAXBContext.newInstance(GlobalNamingResources.class);
StringReader reader = new StringReader("<GlobalNamingResources>\n"
+ " <Resource name=\"UserDatabase\" auth=\"Container\"\n"
+ "\t\ttype=\"org.apache.catalina.UserDatabase\"\n"
+ "\t\tdescription=\"User database that can be updated and saved\"\n"
+ "\t\tfactory=\"org.apache.catalina.users.MemoryUserDatabaseFactory\"\n"
+ "\t\tpathname=\"conf/tomcat-users.xml\" />\n"
+ " </GlobalNamingResources>");
GlobalNamingResources resources = (GlobalNamingResources) context.createUnmarshaller()
.unmarshal(reader);

for (Resource fileInfo : resources.getResource()) {
System.out.println(fileInfo);
}

}

}


실행결과

1
2
3
4
5

Resource(name=UserDatabase, auth=Container, type=org.apache.catalina.UserDatabase, description=User database that can be updated and saved, factory=org.apache.catalina.users.MemoryUserDatabaseFactory, pathname=conf/tomcat-users.xml)

Process finished with exit code 0

잘된다.

참조