XML 파일을 이용해 XPath로 파싱하기
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
35
36
37
38
39
|
package testProcjet;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class XPathTest {
public static void main(String[] args){
try{
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("example.xml");
//XPath 선언
XPath xPath = XPathFactory.newInstance().newXPath();
// 절대경로
Node node = (Node) xPath.evaluate("/bookstore/book/title",document, XPathConstants.NODE);
// Node node = (Node) xPath.evaluate("//book/title",document, XPathConstants.NODE);
System.out.println(node.getTextContent());
}catch(Exception e){
e.printStackTrace();
}
}
}
|
cs |
XML형식의 스트링을 이용해 Xpath로 파싱하기
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package testProcjet;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
public class XPathTest {
public static void main(String[] args){
try{
//xml형식 스트링 값
String xmlString = "<bookstore>"
+ "<book><title>Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book>"
+ "<book><title>Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>"
+ "<book><title>Query Kick Start</title><author>James McGovern</author><author>Per Bothner</author><author>Kurt Cagle</author><author>James Linn</author><author>Vaidyanathan Nagarajan</author><year>2003</year><price>49.99</price></book>"
+ "</bookstore>";
InputSource inputSource = new InputSource(new StringReader(xmlString));
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
//XPath 선언
XPath xPath = XPathFactory.newInstance().newXPath();
// 절대경로
Node node = (Node) xPath.evaluate("/bookstore/book/title",document, XPathConstants.NODE);
// Node node = (Node) xPath.evaluate("//book/title",document, XPathConstants.NODE);
System.out.println(node.getTextContent());
}catch(Exception e){
e.printStackTrace();
}
}
}
|
cs |
|
|
자세한 부분은 밑에 링크를 통해서 학습하세요 ~!
참고 자료
www.w3schools.com/xml/xpath_intro.asp
XPath Tutorial
XPath Tutorial What is XPath? XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath stands for XML Path Language XPath uses "path like" syntax to identify and navigate nodes i
www.w3schools.com
'Java > xml' 카테고리의 다른 글
JAXB example (0) | 2021.01.21 |
---|---|
JAXB 환경 설정 (0) | 2021.01.19 |
XML Schema로 java class 생성 (0) | 2021.01.19 |
XML Schema (0) | 2021.01.19 |
DOM을 이용한XML 파싱 (0) | 2021.01.15 |