1 / 7

XPathAPI 를 이용한 간단한 예제

XPathAPI 를 이용한 간단한 예제. 경상대학교 컴퓨터과학과 조찬제 2005. 01. 05. XPathAPI. HOME : http://xml.apache.org/xalan-j/ API : http://xml.apache.org/xalan-j/apidocs/ Lib. Name : xalan.jar. XPathAPI 에서 사용할 XML 문서. XPathAPI 를 사용한 예제 1. import java.io.IOException; import org.apache.xpath.XPathAPI;

Download Presentation

XPathAPI 를 이용한 간단한 예제

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XPathAPI를 이용한 간단한 예제 경상대학교 컴퓨터과학과 조찬제 2005. 01. 05

  2. XPathAPI • HOME : http://xml.apache.org/xalan-j/ • API : http://xml.apache.org/xalan-j/apidocs/ • Lib. Name : xalan.jar

  3. XPathAPI 에서 사용할 XML문서

  4. XPathAPI를 사용한 예제 1 import java.io.IOException; import org.apache.xpath.XPathAPI; 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; public class XPathTest1 { private static final String URL = "http://blogbridge.naver.com/post/postXMLList.jsp?blogId=kelinkr"; private static final String XPATH = "//item/title/text()| //item/link/text()"; public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document feed = factory.newDocumentBuilder().parse(URL); NodeList titles = XPathAPI.selectNodeList(feed, XPATH); System.out.println("<?xml version=\"1.0\"?>\r\n"); System.out.println("<XPath query=\""+XPATH+"\">\r\n"); for (int i = 0; i < titles.getLength(); i+=2) { System.out.println("\t<title>"+titles.item(i).getNodeValue()+ "</title>\r\n"); System.out.println("\t<link>"+titles.item(i+1).getNodeValue()+ "</link>\r\n"); } System.out.println("</XPath>\r\n"); } }

  5. XPathAPI를 사용한 예제 2 import java.io.IOException; import org.apache.xpath.XPathAPI; 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; public class XPathTest1 { private static final String URL = "http://blogbridge.naver.com/post/postXMLList.jsp?blogId=kelinkr"; private static final String XPATH = "//item/title/text()"; public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document feed = factory.newDocumentBuilder().parse(URL); NodeList titles = XPathAPI.selectNodeList(feed, XPATH); System.out.println("<?xml version=\"1.0\"?>\r\n"); System.out.println("<XPath query=\""+XPATH+"\">\r\n"); for (int i = 0; i < titles.getLength(); i++) { System.out.println("\t<title>"+titles.item(i).getNodeValue()+ "</title>\r\n"); } System.out.println("</XPath>\r\n"); } }

  6. XPathAPI를 사용한 예제 3 public class XPathTest1 { private static final String URL = "http://blogbridge.naver.com/post/postXMLList.jsp?blogId=kelinkr"; private static final String XPATH1 = "//item"; private static final String XPATH2 = "child::title/text()| child::link/text()"; //private static final String XPATH2 = "child::title| child::link"; public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document feed = factory.newDocumentBuilder().parse(URL); NodeList items = XPathAPI.selectNodeList(feed, XPATH1); System.out.println("<?xml version=\"1.0\"?>\r"); System.out.println("<XPath query1=\""+XPATH1+"\" query2=\""+XPATH2+"\">"); for (int i = 0; i < items.getLength(); i++) { NodeList titles = XPathAPI.selectNodeList(items.item(i), XPATH2); System.out.println("\t<item>"); for ( int j = 0 ; j < titles.getLength() ; j += 2 ) { System.out.println("\t\t<title>"+titles.item(j).getNodeValue()+ "</title>"); System.out.println("\t\t<link>"+titles.item(j+1).getNodeValue()+ "</link>"); /* System.out.println("\t\t<title>"+titles.item(j).getTextContent()+ "</title>"); System.out.println("\t\t<link>"+titles.item(j+1).getTextContent()+ "</link>"); */ } System.out.println("\t</item>"); } System.out.println("</XPath>"); } }

  7. XPathAPI를 사용한 예제 3 결과 <?xml version="1.0"?> <XPath query1="//item" query2="child::title/text()| child::link/text()"> 10 <item> <title>부패하지 않는시신...</title> <link>http://blog.naver.com/kelinkr/80008915294</link> </item> <item> <title>다리에 젓꼭지가 자라는 남자..</title> <link>http://blog.naver.com/kelinkr/80008915195</link> </item> <item> <title>연말연시 꼭 이런사람들있다..ㅋㅋㅋ</title> <link>http://blog.naver.com/kelinkr/80008914943</link> </item> <item> <title>반짝..</title> <link>http://blog.naver.com/kelinkr/80008914897</link> </item> <item> <title>종이돈 접기 ^ㅡㅡ^</title> <link>http://blog.naver.com/kelinkr/80008872043</link> </item> <item> <title>1년에 자기연봉?큼 수익을 내었나...제기럴...</title> <link>http://blog.naver.com/kelinkr/80008871822</link> </item> <item> <title>반지의 제왕 성인판[19 금]</title> <link>http://blog.naver.com/kelinkr/80008788605</link> </item> <item> <title>마시마로의 타이완 상륙</title> <link>http://blog.naver.com/kelinkr/80008788546</link> </item> <item> <title>35억짜리 CF</title> <link>http://blog.naver.com/kelinkr/80008788525</link> </item> <item> <title>면목동 고내기 유괴사건..</title> <link>http://blog.naver.com/kelinkr/80008747947</link> </item> </XPath>

More Related