Java XML Document from File read(java.io.File file)

Here you can find the source of read(java.io.File file)

Description

read

License

Open Source License

Declaration

public static org.w3c.dom.Document read(java.io.File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2015, Carnegie Mellon University. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Products derived from the software may not be called "Alice", nor may
 *    "Alice" appear in their name, without prior written permission of
 *    Carnegie Mellon University./*www  .ja va 2 s .co  m*/
 *
 * 4. All advertising materials mentioning features or use of this software must
 *    display the following acknowledgement: "This product includes software
 *    developed by Carnegie Mellon University"
 *
 * 5. The gallery of art assets and animations provided with this software is
 *    contributed by Electronic Arts Inc. and may be used for personal,
 *    non-commercial, and academic use only. Redistributions of any program
 *    source code that utilizes The Sims 2 Assets must also retain the copyright
 *    notice, list of conditions and the disclaimer contained in
 *    The Alice 3.0 Art Gallery License.
 *
 * DISCLAIMER:
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
 * ANY AND ALL EXPRESS, STATUTORY OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,  FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHORS, COPYRIGHT OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING FROM OR OTHERWISE RELATING TO
 * THE USE OF OR OTHER DEALINGS WITH THE SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

public class Main {
    private static javax.xml.parsers.DocumentBuilderFactory s_documentBuilderFactory = null;
    private static javax.xml.parsers.DocumentBuilder s_documentBuilder = null;

    public static org.w3c.dom.Document read(java.io.InputStream is) {
        try {
            //todo?
            javax.xml.parsers.DocumentBuilder documentBuilder = getDocumentBuilder();
            synchronized (documentBuilder) {
                org.w3c.dom.Document rv = documentBuilder.parse(is);
                // TODO: we should validate this document with a schema and call DocumentBuilderFactory.setIgnoringElementContentWhitespace() instead.
                // but this work-around will at least allow us to load files that have empty white space.
                removeWhitespaceNodes(rv.getDocumentElement());
                return rv;
            }
        } catch (java.io.IOException ioe) {
            throw new RuntimeException(ioe);
        } catch (org.xml.sax.SAXException saxe) {
            throw new RuntimeException(saxe);
        }
    }

    public static org.w3c.dom.Document read(java.io.File file) {
        try {
            return read(new java.io.FileInputStream(file));
        } catch (java.io.IOException ioe) {
            throw new RuntimeException(file.toString(), ioe);
        }
        //      try {
        //         //todo?
        //         javax.xml.parsers.DocumentBuilder documentBuilder = getDocumentBuilder();
        //         synchronized( documentBuilder ) {
        //            org.w3c.dom.Document rv = documentBuilder.parse( file );
        //            return rv;
        //         }
        //      } catch( java.io.IOException ioe ) {
        //         throw new RuntimeException( file.toString(), ioe );
        //      } catch( org.xml.sax.SAXException saxe ) {
        //         throw new RuntimeException( file.toString(), saxe );
        //      }
    }

    public static org.w3c.dom.Document read(String path) {
        return read(new java.io.File(path));
    }

    private static javax.xml.parsers.DocumentBuilder getDocumentBuilder() {
        if (s_documentBuilder != null) {
            //pass
        } else {
            try {
                s_documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
            } catch (javax.xml.parsers.ParserConfigurationException pce) {
                throw new RuntimeException(pce);
            }
        }
        return s_documentBuilder;
    }

    @Deprecated
    private static void removeWhitespaceNodes(org.w3c.dom.Element e) {
        org.w3c.dom.NodeList children = e.getChildNodes();
        for (int i = children.getLength() - 1; i >= 0; i--) {
            org.w3c.dom.Node child = children.item(i);
            if ((child instanceof org.w3c.dom.Text)
                    && (((org.w3c.dom.Text) child).getData().trim().length() == 0)) {
                e.removeChild(child);
            } else if (child instanceof org.w3c.dom.Element) {
                removeWhitespaceNodes((org.w3c.dom.Element) child);
            }
        }
    }

    private static javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory() {
        if (s_documentBuilderFactory != null) {
            //pass
        } else {
            s_documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        }
        return s_documentBuilderFactory;
    }
}

Related

  1. loadXMLDocument(String path)
  2. loadXmlDocumentFromFile(@Nonnull File file)
  3. loadXmlFromFile(String xmlFileName)
  4. read(InputStream input)
  5. read(InputStream xml)
  6. read(String file)
  7. read(String filename)
  8. readDocument(String fileName)
  9. readDocumentXML(File file)