250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- spring
- Spring Boot
- maven
- MySQL
- Eclipse
- PostgreSQL
- Open Source
- Tomcat
- STS
- error
- MSSQL
- Source
- Core Java
- ubuntu
- JavaScript
- 오픈소스
- myBatis
- Docker
- SpringBoot
- Thymeleaf
- IntelliJ
- git
- Python
- 설정
- oracle
- jpa
- JDBC
- Exception
- AJAX
- 문서
Archives
- Today
- Total
헤르메스 LIFE
[Source] XPatharserDemo 본문
728x90
출처 : http://www.tutorialspoint.com/java_xml/java_jdom_parse_document.htm
dinkar kad dinkar 85 Vaneet Gupta vinni 95 jasvir singh jazz 90
package com.file .utils; import java. io.File ; import java. io.IOException ; import javax. xml.parsers .DocumentBuilder; import javax. xml.parsers .DocumentBuilderFactory; import javax. xml.parsers .ParserConfigurationException; import javax. xml.xpath .XPath; import javax. xml.xpath .XPathConstants; import javax. xml.xpath .XPathExpressionException; import javax. xml.xpath .XPathFactory; import org. w3c.dom .Document; import org. w3c.dom .Element; import org. w3c.dom .Node; import org. w3c.dom .NodeList; import org. xml.sax .SAXException; public class XPathParserDemo { public static void main( String[] args ) { try { File inputFile = new File ("d:/xPathTest.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory. newInstance(); DocumentBuilder dBuilder ; dBuilder = dbFactory. newDocumentBuilder(); Document doc = dBuilder.parse (inputFile) ; doc.getDocumentElement ().normalize (); // Build XPath XPath xPath = XPathFactory.newInstance() .newXPath() ; String expression = "/class/student" ; NodeList nodeList = (NodeList) xPath.compile (expression) .evaluate( doc, XPathConstants. NODESET); for ( int i = 0; i < nodeList. getLength(); i++ ) { Node nNode = nodeList.item (i) ; System.out. println("\nCurrent Element :" + nNode.getNodeName ()); if ( nNode.getNodeType () == Node.ELEMENT_NODE ) { Element eElement = (Element) nNode; System.out. println("Student roll no : " + eElement.getAttribute ("rollno")) ; System.out. println("First Name : " + eElement.getElementsByTagName ("firstname") .item( 0). getTextContent()); System.out. println("Last Name : " + eElement.getElementsByTagName ("lastname") .item( 0). getTextContent()); System.out. println("Nick Name : " + eElement.getElementsByTagName ("nickname") .item( 0). getTextContent()); System.out. println("Marks : " + eElement.getElementsByTagName ("marks") .item( 0). getTextContent()); } } } catch (ParserConfigurationException e) { e.printStackTrace (); } catch (SAXException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } catch (XPathExpressionException e) { e.printStackTrace (); } } }
Current Element :student Student roll no : 393 First Name : dinkar Last Name : kad Nick Name : dinkar Marks : 85
Current Element :student Student roll no : 493 First Name : Vaneet Last Name : Gupta Nick Name : vinni Marks : 95
Current Element :student Student roll no : 593 First Name : jasvir Last Name : singh Nick Name : jazz Marks : 90 |
728x90
'Core Java' 카테고리의 다른 글
[Source] Create Xml File Demo (0) | 2020.12.20 |
---|---|
[Source] DOM4j Modify XMLDemo (0) | 2020.12.20 |
[HttpURLConnection] jsp 파일의 실행 (0) | 2020.12.17 |
외부프로그램 실행시키기 (0) | 2020.12.17 |
Thread 를 이용한 데몬 프로그램 (0) | 2020.12.17 |