본문 바로가기

공부/컴퓨터

XPath 를 이용한 간단한 예제

반응형
XPathAPI를 이용한 간단한 예제 입니다.
첨부화일은 ppt 자료로 되어 있습니다. ^_^
XPathAPI를 두번 사용해서. 자료를 꺼내는것에 대한 예제 입니다. ^_^



/*
* 작성된 날짜: 2005. 1. 5.
* 만  든  이 : Chan ( http://ggaman.com , chan@ggaman.com )
* 프로젝트명 : XPathTest
* 패키지이름 :
* 화  일  명 : XPathTest1.java
*
* 하  는  일 : {{ 하는 일을 적으세요 ^_^ }}
*
*/

import java.io.IOException;

import org.apache.xpath.XPathAPI;
import org.apache.xpath.objects.XString;
import org.apache.xpath.objects.XBoolean;
import org.apache.xpath.objects.XNumber;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.objects.XNull;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import org.apache.xpath.objects.XNodeSet;
/**
* @author Chan ( http://ggaman.com , chan@ggaman.com )
*
*/
public class XPathTest1 {

    // URL of Slashdot's RSS feed.
    private static final String URL = "http://slashdot.org/index.rss";
    /*
        <rdf:RDF
          xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
          xmlns="http://purl.org/rss/1.0/"
          xmlns:dc="http://purl.org/dc/elements/1.1/">
        
         <channel rdf:about="http://slashdot.org/">
          <title>Slashdot</title>
          <link>http://slashdot.org/</link>
          <description>News for nerds, stuff that matters</description>
        
          <items>
           <rdf:Seq>
            <rdf:li rdf:resource="http://slashdot.org/article.pl?sid=04/05/08/2254227" />
            <rdf:li rdf:resource="http://slashdot.org/article.pl?sid=04/05/08/2210224" />
            <rdf:li rdf:resource="http://slashdot.org/article.pl?sid=04/05/08/1747258" />
           </rdf:Seq>
          </items>
         </channel>
        
         <item rdf:about="http://slashdot.org/article.pl?sid=04/05/08/2254227">
          <title>What's Being Done About Nuclear Security</title>
          <link>http://slashdot.org/article.pl?sid=04/05/08/2254227</link>
          <description>KrisCowboy writes "Wired.com has an interesting article ... </description>
          <dc:subject>security</dc:subject>
         </item>
        
         <item rdf:about="http://slashdot.org/article.pl?sid=04/05/08/2210224">
          <title>Cyber-Soap Returns From The Dead</title>
          <link>http://slashdot.org/article.pl?sid=04/05/08/2210224</link>
          <description>An anonymous reader submits "Back in 1995, an experimental ...</description>
          <dc:subject>ent</dc:subject>
         </item>
        
         <item rdf:about="http://slashdot.org/article.pl?sid=04/05/08/1747258">
          <title>Phatbot Author Arrested In Germany</title>
          <link>http://slashdot.org/article.pl?sid=04/05/08/1747258</link>
          <description>Tacito writes "After arresting the author of Sasser, the ...</description>
          <dc:subject>security</dc:subject>
         </item>
        </rdf:RDF>
          
     */
    
    // XPath expression that selects text content of titles of articles.
    private static final String XPATH = "RDF/item/title/text()";
    
    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException {

        // Parse feed into DOM tree.
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document feed = factory.newDocumentBuilder().parse(URL);

        // Select article titles into DOM node list.
        NodeList titles = XPathAPI.selectNodeList(feed, XPATH);

        // Iterate over node list and print article titles.
        for (int i = 0; i < titles.getLength(); i++) {
            System.out.println(titles.item(i).getNodeValue());
        }
    }

}


반응형