우아한 프로그래밍

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.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

 

public class XMLParser {

        private static final String CONST_PRE_PIX = "//";

        private DocumentBuilderFactory factory;

        private DocumentBuilder builder;

        private Document document;

        private XPathFactory xPathfactory;

        private XPath xpath;

       

       

        public XMLParser() {

               try {

                       factory = DocumentBuilderFactory.newInstance();

                       builder = factory.newDocumentBuilder();

                       xPathfactory = XPathFactory.newInstance();

                       xpath = xPathfactory.newXPath();

                      

               } catch (ParserConfigurationException e) {

                       e.printStackTrace();

               }

        }

       

        /**

         * @param path

         * @throws Exception

         * XML 파일 경로

         */

        public void setXML(String path) throws Exception {

                try {

                       document = builder.parse("D:/env/workspace/TEST/Test01/temp/test.xml");

               } catch (SAXException e) {            

                       throw new Exception("XML파일이 정상적인 포맷이 아닙니다.");

               } catch (IOException e) {

                       throw new Exception("해당 경로에 XML파일을 찾을 수 없습니다.");

               }             

        }

 

        /**

         * @param path

         * @return

         * @throws Exception

         * 리스트 형태의 노드 획득

         */

        public NodeList getNodeList(String path) throws Exception {

               NodeList nodeList = null;             

               try {

                       nodeList = (NodeList) xpath.evaluate( CONST_PRE_PIX + path, document, XPathConstants.NODESET);

               } catch (XPathExpressionException e) {

                       throw new Exception("해당 표현식에 해당하는 경로가 없습니다.");

               }

               return nodeList;

        }

       

        /**

         * @param path

         * @return

         * @throws Exception

         * 하나의 노드 획득

         */

        public Node getNode(String path) throws Exception {

               Node node = null;             

               try {

                       node = (Node) xpath.evaluate( CONST_PRE_PIX +  path, document, XPathConstants.NODE);

               } catch (XPathExpressionException e) {

                       throw new Exception("해당 표현식에 해당하는 경로가 없습니다.");

               }

               return node;

        }

 

        /**

         * @param path

         * @return

         * @throws Exception

         * 노드의 텍스트 획득

         */

        public String getText(String path) throws Exception {

               String text = null;           

               try {

                        text = (String) xpath.evaluate( CONST_PRE_PIX + path, document, XPathConstants.STRING);

               } catch (XPathExpressionException e) {

                       throw new Exception("해당 표현식에 해당하는 경로가 없습니다.");

               }

               return text;

        }

}



profile

우아한 프로그래밍

@자바조아!

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!