Java XML Document to String xmlDocumentDecode(final String xmlURI)

Here you can find the source of xmlDocumentDecode(final String xmlURI)

Description

Xml document decode.

License

BSD License

Parameter

Parameter Description
xmlURI the xml uri

Exception

Parameter Description
Exception the exception

Return

the element

Declaration

public static final Element xmlDocumentDecode(final String xmlURI)
        throws Exception 

Method Source Code

//package com.java2s;
/**/*from   w  w w.java2  s  .c  o  m*/
 * Copyright (C) 2011-2012 Barchart, Inc. <http://www.barchart.com/>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */

import java.io.ByteArrayInputStream;
import java.io.InputStream;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    private static final ThreadLocal<DocumentBuilder> XML_BUILDER = new ThreadLocal<DocumentBuilder>() {
        @Override
        protected DocumentBuilder initialValue() {
            try {

                final DocumentBuilder builder = DocumentBuilderFactory
                        .newInstance().newDocumentBuilder();

                return builder;

            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };

    /**
     * Xml document decode.
     * 
     * @param xmlURI
     *            the xml uri
     * @return the element
     * @throws Exception
     *             the exception
     */
    public static final Element xmlDocumentDecode(final String xmlURI)
            throws Exception {
        return XML_BUILDER.get().parse(xmlURI).getDocumentElement();
    }

    /**
     * Xml dodument decode.
     * 
     * @param array
     *            the array
     * @param start
     *            the start
     * @param finish
     *            the finish
     * @param isThrow
     *            the is throw
     * @return the element
     */
    public static final Element xmlDocumentDecode(final byte[] array,
            final int start, final int finish, final boolean isThrow) {
        final InputStream stream = new ByteArrayInputStream(array, start,
                finish);
        try {
            final Document document = XML_BUILDER.get().parse(stream);
            return document.getDocumentElement();
        } catch (final Exception e) {
            // will return/throw below
        }
        if (isThrow) {
            throw new RuntimeException("can not decode : \n"
                    + new String(array));
        }
        return null;
    }
}

Related

  1. writeToBytes(Document document)
  2. writeXML(Document d)
  3. writeXmlToString(Document xmlDoc)
  4. xmlDocToString(Document doc)
  5. xmlDocToString(Document doc)
  6. xmlDocumentToString(Document doc)
  7. xmlDocumentToString(Document doc)
  8. xmlDocumentToString(Document doc)
  9. xmlDocumentToString(Document document)