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

황제낙엽 2006.08.31 20:52 조회 수 : 787 추천: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;
        }
번호 제목 글쓴이 날짜 조회 수
86 [BPP] 게시판 페이징 로직 분석 - (2) 간단한 페이징 기법 황제낙엽 2007.06.08 996
85 [BPP] 게시판 페이징 로직 분석 - (1) 클래스 목록 file 황제낙엽 2007.06.06 576
84 문자열의 앞뒤space제거처리 성능체크 황제낙엽 2007.06.03 547
83 간단한 서블릿 예제들 file 황제낙엽 2007.05.12 522
82 [Tip] 톰캣 JNDI DB POOL 설정하기 황제낙엽 2007.05.11 935
81 [javac 에러] code too large for try statement 황제낙엽 2007.02.28 902
80 JDBC 테스트 페이지 file 황제낙엽 2007.02.22 9879
79 [JDBC] URL 사용법 모음 황제낙엽 2007.02.21 2187
78 Exception Tunneling - C2 Wiki (last edited January 15, 2005) 황제낙엽 2007.02.08 1319
77 Java's checked exceptions were a mistake - Rod Waldhoff (1 April 2003) 황제낙엽 2007.02.08 817
76 Best Practices for Exception Handling by Gunjan Doshi (11/19/2003) 황제낙엽 2007.02.08 599
75 "Thinking in Java"의 저자인 Bruce Eckel의 Checked Exception에 대한 생각 황제낙엽 2007.02.06 634
74 수치형 연산 고려사항 황제낙엽 2007.01.25 672
73 시스템 속성(System Property) 얻기 및 설정하기 황제낙엽 2007.01.24 742
72 자바코딩을 위한 EditPlus설정법 황제낙엽 2003.05.16 693
71 SCJP 2002년 10월 정도의 버전 덤프들입니다. 황제낙엽 2002.12.29 767
70 Jakarta 프로젝트의 Regexp(정규식) 패키지 사용하기 황제낙엽 2007.01.22 662
69 PATTERN MATCHING (패턴 매칭) file 황제낙엽 2007.01.17 940
» JAVA 와 XML (간단한 프로그램 예제) 황제낙엽 2006.08.31 787
67 getServerPort(), getLocalPort(), getRemotePort() 황제낙엽 2006.08.07 683