Description
Parses an XML document into a DOM tree.
License
Open Source License
Parameter
Parameter | Description |
---|
input | a parser input (for URL users use: <code>new InputSource(url.toString())</code> |
validate | if true validating parser is used |
namespaceAware | if true DOM is created by namespace aware parser |
errorHandler | a error handler to notify about exception (such as #defaultErrorHandler ) or <code>null</code> |
entityResolver | SAX entity resolver (such as EntityCatalog#getDefault) or <code>null</code> |
Exception
Parameter | Description |
---|
IOException | if an I/O problem during parsing occurs |
SAXException | is thrown if a parser error occurs |
FactoryConfigurationError | Application developers should never need to directly catch errors of this type. |
Return
document representing given input
Declaration
public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException
Method Source Code
//package com.java2s;
/*//from w w w.j a va2 s . c o m
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Main {
private static DocumentBuilderFactory[][] doms = new DocumentBuilderFactory[2][2];
/**
* Parses an XML document into a DOM tree.
*
* <div class="nonnormative">
*
* <p>Remember that when parsing XML files you often want to set an explicit
* entity resolver. For example, consider a file such as this:</p>
*
* <pre>
* <?<font class="keyword">xml</font> <font class="variable-name">version</font>=<font class="string">"1.0"</font> <font class="variable-name">encoding</font>=<font class="string">"UTF-8"</font>?>
* <!<font class="keyword">DOCTYPE</font> <font class="type">root</font> <font class="keyword">PUBLIC</font> <font class="string">"-//NetBeans//DTD Foo 1.0//EN"</font> <font class="string">"http://www.netbeans.org/dtds/foo-1_0.dtd"</font>>
* <<font class="function-name">root</font>/>
* </pre>
*
* <p>If you parse this with a null entity resolver, or you use the
* default resolver (EntityCatalog.getDefault) but do not do
* anything special with this DTD, you will probably find the parse
* blocking to make a network connection <em>even when you are not
* validating</em>. That is because DTDs can be used to define
* entities and other XML oddities, and are not a pure constraint
* language like Schema or RELAX-NG.</p>
*
* <p>There are three basic ways to avoid the network connection.</p>
*
* <ol>
*
* <li><p>Register the DTD. This is generally the best thing to do. See
* EntityCatalog's documentation for details, but for example
* in your layer use:</p>
*
* <pre>
* <<font class="function-name">filesystem</font>>
* <<font class="function-name">folder</font> <font class="variable-name">name</font>=<font class="string">"xml"</font>>
* <<font class="function-name">folder</font> <font class="variable-name">name</font>=<font class="string">"entities"</font>>
* <<font class="function-name">folder</font> <font class="variable-name">name</font>=<font class="string">"NetBeans"</font>>
* <<font class="function-name">file</font> <font class="variable-name">name</font>=<font class="string">"DTD_Foo_1_0"</font>
* <font class="variable-name">url</font>=<font class="string">"resources/foo-1_0.dtd"</font>>
* <<font class="function-name">attr</font> <font class="variable-name">name</font>=<font class="string">"hint.originalPublicID"</font>
* <font class="variable-name">stringvalue</font>=<font class="string">"-//NetBeans//DTD Foo 1.0//EN"</font>/>
* </<font class="function-name">file</font>>
* </<font class="function-name">folder</font>>
* </<font class="function-name">folder</font>>
* </<font class="function-name">folder</font>>
* </<font class="function-name">filesystem</font>>
* </pre>
*
* <p>Now the default system entity catalog will resolve the public ID
* to the local copy in your module, not the network copy.
* Additionally, anyone who mounts the "NetBeans Catalog" in the XML
* Entity Catalogs node in the Runtime tab will be able to use your
* local copy of the DTD automatically, for validation, code
* completion, etc. (The network URL should really exist, though, for
* the benefit of other tools!)</p></li>
*
* <li><p>You can also set an explicit entity resolver which maps that
* particular public ID to some local copy of the DTD, if you do not
* want to register it globally in the system for some reason. If
* handed other public IDs, just return null to indicate that the
* system ID should be loaded.</p></li>
*
* <li><p>In some cases where XML parsing is very
* performance-sensitive, and you know that you do not need validation
* and furthermore that the DTD defines no infoset (there are no
* entity or character definitions, etc.), you can speed up the parse.
* Turn off validation, but also supply a custom entity resolver that
* does not even bother to load the DTD at all:</p>
*
* <pre>
* <font class="keyword">public</font> <font class="type">InputSource</font> <font class="function-name">resolveEntity</font>(<font class="type">String</font> <font class="variable-name">pubid</font>, <font class="type">String</font> <font class="variable-name">sysid</font>)
* <font class="keyword">throws</font> <font class="type">SAXException</font>, <font class="type">IOException</font> {
* <font class="keyword">if</font> (pubid.equals(<font class="string">"-//NetBeans//DTD Foo 1.0//EN"</font>)) {
* <font class="keyword">return</font> <font class="keyword">new</font> <font class="type">InputSource</font>(<font class="keyword">new</font> <font class="type">ByteArrayInputStream</font>(<font class="keyword">new</font> <font class="type">byte</font>[0]));
* } <font class="keyword">else</font> {
* <font class="keyword">return</font> EntityCatalog.getDefault().resolveEntity(pubid, sysid);
* }
* }
* </pre></li>
*
* </ol>
*
* </div>
*
* @param input a parser input (for URL users use: <code>new InputSource(url.toString())</code>
* @param validate if true validating parser is used
* @param namespaceAware if true DOM is created by namespace aware parser
* @param errorHandler a error handler to notify about exception (such as {@link #defaultErrorHandler}) or <code>null</code>
* @param entityResolver SAX entity resolver (such as EntityCatalog#getDefault) or <code>null</code>
*
* @throws IOException if an I/O problem during parsing occurs
* @throws SAXException is thrown if a parser error occurs
* @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
*
* @return document representing given input
*/
public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {
DocumentBuilder builder = null;
DocumentBuilderFactory factory = getFactory(validate, namespaceAware);
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N
}
if (errorHandler != null) {
builder.setErrorHandler(errorHandler);
}
if (entityResolver != null) {
builder.setEntityResolver(entityResolver);
}
return builder.parse(input);
}
private static synchronized DocumentBuilderFactory getFactory(boolean validate, boolean namespaceAware) {
DocumentBuilderFactory factory = doms[validate ? 0 : 1][namespaceAware ? 0 : 1];
if (factory == null) {
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validate);
factory.setNamespaceAware(namespaceAware);
doms[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory;
}
return factory;
}
}
Related
- parse(final InputSource is, final Class loader)
- parse(InputSource in)
- parse(InputSource source)
- parseXML(InputSource is)