xmlconnect.java

来自「一个java 代码生成器」· Java 代码 · 共 116 行

JAVA
116
字号
/**
 * Copyright (c) 2002, Siddhartha P. Chandurkar siddhartha@visioncodified.com
 * All rights reserved.
 * Licensed under the Academic Free License version 1.1
 * See the file LICENSE.TXT for details.
 * LICENSE.txt is located in the directory  <install-directory>\Jenerator
 * of your Jenertaor Installation.
 *
 */
package com.jenerator.util;


/* <Imports> */

import org.apache.log4j.Logger;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;

/* </Imports> */

/**
 * Class XMLConnect
 * Description goes here
 * This class is used to parse a given XML doucment and convert
 * it into a DOM doucment.
 * @version 0.9.0
 * @author  Siddhartha P. Chandurkar
 */

public class XMLConnect {
    //ATTRIBUTES
    //The URI of the XML file to be parsed
    private String uri;
    //The DOM document of the XML file
    private Document doc;

    private Logger log = Logger.getLogger(com.jenerator.util.XMLConnect.class.getName());

    //CONSTRUCTOR
    /**
     * Constructor declaration
     *
     * @param _uri an uri to the xml file.
     */

    public XMLConnect(String _uri) {
        uri = _uri;

    }

    //PUBLIC METHODS
    /**
     * Method read
     * Parses the document set by constructor.
     *
     * prints out any encountered exception with stack-trace and rethrows it.
     *
     *
     * @exception if no document, or error in parsing it.
     */

    public Document read() throws ConfigurationException {
        try {
            log.info("Reading XML " + uri);
            DOMParser parser = new DOMParser();
            parser.parse(new InputSource(new FileInputStream(uri)));
            doc = parser.getDocument();

        } catch (java.io.FileNotFoundException e) {
            log.fatal(e);
            throw new ConfigurationException(e);
        } catch (java.io.IOException e) {
            log.fatal(e);
            throw new ConfigurationException(e);
        } catch (SAXException e) {
            log.fatal(e);
            throw new ConfigurationException(e);
        } catch (Exception e) {
            log.fatal(e);
            throw new ConfigurationException(e);
        }

        return doc;
    }

    public Document readXmlString() throws SAXException, IOException {


        try {
            DOMParser parser = new DOMParser();
            //System.out.println("THE URI XMLConnct : " + uri);
            parser.parse(new InputSource(new StringReader(uri)));
            doc = parser.getDocument();
        } catch (SAXException e) {
            log.fatal(e);
            throw e;
        } catch (IOException e) {
            log.fatal(e);
            throw e;
        }
        //System.out.println("Gotcha" + doc);


        return doc;
    }
}//XMLConnect


//XMLConnect.java

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?