일반 JAVA 와 XML (간단한 프로그램 예제)

황제낙엽 2006.08.31 20:52 조회 수 : 462 추천:230

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  
        /**
         * 생성한 Document 객체를 XML 파일로 저장하는 메서드 parameter로 file이름을 받는다
         */

        public void saveXmlFile(String fileName) {
                
                if (fileName == null || fileName.length() == 0) {
                        // Error!! 입력값이 없슴
                        // 에러 처리
                }
                
                try {
                        java.io.FileOutputStream fileoutputstream = new java.io.FileOutputStream(new File(fileName));

                        org.apache.xml.serialize.OutputFormat outputformat = new org.apache.xml.serialize.OutputFormat();
                        outputformat.setEncoding("UTF-8");

                        outputformat.setDoctype(
                                                        "-//KH//DTD Korean History Potal System v.0.1//EN",                                                                 // Document
                                                        "http://192.168.1.242:8101/warmemo/jsp/khcollection/dataprovider/metadata.dtd");         // Type설정

                        outputformat.setIndent(4);
                        outputformat.setMethod("xml");
                        outputformat.setIndenting(true);
                        outputformat.setPreserveSpace(false);

                        org.apache.xml.serialize.XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer();

                        serializer.setOutputFormat(outputformat);
                        serializer.setOutputByteStream(fileoutputstream);
                        serializer.asDOMSerializer();
                        serializer.serialize(getDocument().getDocumentElement());
                        fileoutputstream.flush();
                        fileoutputstream.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        /**
         * Document 객체를 생성하는 메서드
         */

        public org.w3c.dom.Document getDocument() {
                org.apache.xerces.dom.DocumentImpl doc = new org.apache.xerces.dom.DocumentImpl();

                org.w3c.dom.Element root = null;
                org.w3c.dom.Element item1 = null;
                org.w3c.dom.Element item2 = null;

                // root Element create.
                root = doc.createElement("root");
                root.setAttribute("ROOT_ATTR1", "rootAttr1");
                root.setAttribute("ROOT_ATTR2", "rootAttr2");

                // First Element
                item1 = doc.createElement("item1");
                item1.setAttribute("ITEM1_ATTR", "item1Attr");

                // Second Element
                item2 = doc.createElement("item2");
                item2.setAttribute("ITEM2_ATTR1", "item2Attr1");
                item2.setAttribute("ITEM2_ATTR2", "item2Attr2");

                item1.appendChild(item2);
                root.appendChild(item1);

                return doc;
        }

        /**
         * XML 화일로부터 Dom 노드를 읽어온다.
         */

        static org.w3c.dom.Node parse(String fileName) {
                if (fileName == null || fileName.length() == 0) {
                        // Error!! 입력값이 없슴
                        // 에러 처리
                }
                
                // 화일이름을 URI형식으로 변환한다.
                String uri = "file:///" + fileName;

                // URI로부터 ROOT 노드를 읽어온다.
                org.w3c.dom.Node root = null;        
                try {
                        javax.xml.parsers.DocumentBuilderFactory docBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
                        javax.xml.parsers.DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

                        org.xml.sax.InputSource inputFile = new org.xml.sax.InputSource(uri);
                        org.w3c.dom.Document document = docBuilder.parse(inputFile);
                        root = document.getDocumentElement();
                } catch (org.xml.sax.SAXParseException saxParseEx) {
                        // SAXParseException 발생
                } catch (org.xml.sax.SAXException saxEx) {
                        // SAXException 발생
                } catch (java.lang.Exception e) {
                        // 기타 Exception 발생
                }
                return root;
        }
번호 제목 글쓴이 날짜 조회 수
83 [Tip] 톰캣 JNDI DB POOL 설정하기 황제낙엽 2007.05.11 487
82 [javac 에러] code too large for try statement 황제낙엽 2007.02.28 545
81 JDBC 테스트 페이지 file 황제낙엽 2007.02.22 9266
80 [JDBC] URL 사용법 모음 황제낙엽 2007.02.21 1499
79 Exception Tunneling - C2 Wiki (last edited January 15, 2005) 황제낙엽 2007.02.08 1052
78 Java's checked exceptions were a mistake - Rod Waldhoff (1 April 2003) 황제낙엽 2007.02.08 609
77 Best Practices for Exception Handling by Gunjan Doshi (11/19/2003) 황제낙엽 2007.02.08 370
76 "Thinking in Java"의 저자인 Bruce Eckel의 Checked Exception에 대한 생각 황제낙엽 2007.02.06 383
75 수치형 연산 고려사항 황제낙엽 2007.01.25 473
74 시스템 속성(System Property) 얻기 및 설정하기 황제낙엽 2007.01.24 542
73 자바코딩을 위한 EditPlus설정법 황제낙엽 2003.05.16 429
72 SCJP 2002년 10월 정도의 버전 덤프들입니다. 황제낙엽 2002.12.29 546
71 Jakarta 프로젝트의 Regexp(정규식) 패키지 사용하기 황제낙엽 2007.01.22 286
70 PATTERN MATCHING (패턴 매칭) file 황제낙엽 2007.01.17 521
» JAVA 와 XML (간단한 프로그램 예제) 황제낙엽 2006.08.31 462
68 getServerPort(), getLocalPort(), getRemotePort() 황제낙엽 2006.08.07 449
67 컨텐츠 타입에 대해서 황제낙엽 2006.08.07 516
66 서블릿의 생명주기 file 황제낙엽 2006.08.07 355
65 서블릿의 초기화 황제낙엽 2006.07.24 292
64 JSTL 황제낙엽 2006.02.17 520