자바에서 XPath 사용 하기

 

XPath란 

 


 

 XPath 는 XML문서에서 특정 부분의 위치를 찾는데 사용하는 개념이다. 쉽게 표현하면 XML 문서를 트리 구조로 표현하고, 최상위 노드부터 최하위 노드까지의 모든 노드 엘리먼트과 속성 그리고 데이터를 추출할 수 있는 경로를 나타낸다. XML 문서 검색과 XSL/XSLT에서 가장 많이 사용하며, XLink나 XPointer에서 필수적으로 사용하는 개념이다.

 

 

XPath 노드

 


 

root node: XML문서를 표현하는 모든 계층적 노드의 최상위 노드이다.

element node: XML문서안의 각각의 엘리먼트

attribute node: XML문서안의 각 속성값

text node: 엘리먼트노드의 텍스트 컨텐츠

namespace node: 한 엘리먼트가 속해 있는 네임스페이스


<sudents>

   <student id="1234">

        <name>

              홍길동

        </name>

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

        <phone>

               010-1111-2222

        </phone>

   </student>

</students>

 

name의 위치에 대한 XPath 표현식

/student/name

 

id에 대한 XPath표현식

/students/student/@id            <-- 절대경로

student/@id                             <-- 상대경로

 

 

[링크] XPath 문법

 

 

 

자바에서 XPath 사용하기

 


 

     public class XPathTest extends TestCase {

    public void testXPathExpression() throws Exception {
        final String xml = 
            "" +
            "" +
            "    " +
            "        " +
            "        Goetz" +
            "    " +
            "    " +
            "        " +
            "        Martin Fowler" +
            "    " +
            "";
        
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document xmlDoc = builder.parse(new InputSource(new StringReader(xml)));
        
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        XPathExpression expr = xPath.compile("/books/book[title='Refactoring']/author/text()");
        
        Object result = expr.evaluate(xmlDoc, XPathConstants.NODE);
        Node node = (Node)result;
        
        assertEquals("Martin Fowler", node.getTextContent());
    }
    
}

[출처] http://www.openeg.co.kr/286

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
18 [java 인공지능] Spring AI 로 ChatGPT API 만들기 졸리운_곰 2024.12.30 214
17 [java 인공지능] 오라클, 자바 머신러닝 라이브러리 ‘트리뷰오’ 오픈소스로 공개 졸리운_곰 2023.08.27 296
16 [java 인공지능] 자바를 위한 머신 러닝 라이브러리 졸리운_곰 2023.08.27 360
15 [Java 인공지능] 오라클, 자바 머신러닝 라이브러리 ‘트리뷰오’ 오픈소스로 공개 file 졸리운_곰 2023.08.13 358
14 [java 인공지능] [java] 라이프 게임 (life game) file 졸리운_곰 2021.10.19 340
13 How to use Weka in your Java code 졸리운_곰 2020.02.01 307
12 weka and java eclipse example : A Simple Machine Learning Example in Java file 졸리운_곰 2020.01.31 362
11 머신러닝? weka file 졸리운_곰 2020.01.31 357
10 [Weka] Weka를 이용한 Iris 데이터 머신러닝 file 졸리운_곰 2020.01.30 363
9 [강좌] WEKA 사용법 (간단한 분류, 의사결정트리 분석 설명) file 졸리운_곰 2020.01.30 343
8 [JESS] Jess , 이클립스 연동 file 졸리운_곰 2019.12.22 215
7 Jess 간단한 문법 요약 졸리운_곰 2019.12.22 277
6 Jess 6.1 다운로드 friedman-hill_src_1_jess_se file 졸리운_곰 2019.12.22 184
5 java artificial intelligence Rule Engine Jess Working Memory 졸리운_곰 2019.12.22 300
4 Defining Functions in Jess 졸리운_곰 2019.12.22 389
3 Jess Language Basics 졸리운_곰 2019.12.22 273
2 Embedding Jess in a Java Application 졸리운_곰 2019.12.22 299
1 다섯개의 탑 자바로 머신러닝 라이브러리 Top 5 machine learning libraries for Java file 졸리운_곰 2017.08.22 388
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED