edu.scripps.fl.pubchem.promiscuity.XMLDocument.java Source code

Java tutorial

Introduction

Here is the source code for edu.scripps.fl.pubchem.promiscuity.XMLDocument.java

Source

/*               
 * Copyright 2011 The Scripps Research Institute               
 *               
 * Licensed under the Apache License, Version 2.0 (the "License");               
 * you may not use this file except in compliance with the License.               
 * You may obtain a copy of the License at               
 *               
 *     http://www.apache.org/licenses/LICENSE-2.0               
 *               
 * Unless required by applicable law or agreed to in writing, software               
 * distributed under the License is distributed on an "AS IS" BASIS,               
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.               
 * See the License for the specific language governing permissions and               
 * limitations under the License.               
 */
package edu.scripps.fl.pubchem.promiscuity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Document;
import org.dom4j.io.DOMReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

public class XMLDocument {

    public Document readDocFromURL(URL url) throws ParserConfigurationException, IOException, SAXException {
        InputStream stream = url.openStream();
        return readDocFromStream(stream);
    }

    public Document readDocFromStream(InputStream stream)
            throws SAXException, IOException, ParserConfigurationException {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        org.w3c.dom.Document doc2 = builder.parse(stream);
        DOMReader reader = new DOMReader();
        Document doc = reader.read(doc2);
        return doc;
    }

    public void write(Document doc, File toFile) throws IOException {
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter(toFile), format);
        writer.write(doc);
        writer.close();
    }

}