Java XML Element Namespace byteArrayToElement(byte[] b, boolean namespaceAware)

Here you can find the source of byteArrayToElement(byte[] b, boolean namespaceAware)

Description

Convert the byte array representation of an Element into an Element

License

Open Source License

Parameter

Parameter Description

Return

Element

Declaration

public static Element byteArrayToElement(byte[] b, boolean namespaceAware)
        throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  www. j a  v a2 s  .  c o m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Convert the byte array representation of an Element into an Element
     * @param byte[] representation of an Element.
     * @param boolean set whether the return Element should be namespace aware.
     * @return Element
     */
    public static Element byteArrayToElement(byte[] b, boolean namespaceAware)
            throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(namespaceAware);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new ByteArrayInputStream(b));
        return doc.getDocumentElement();
    }
}

Related

  1. addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI)
  2. addNamespacePrefix(Element element, String namespaceUri, String prefix)
  3. buildNamespacePrefixMap(final Element root)
  4. buildNamespacePrefixMap(final Element root)
  5. createElement(final String namespace, final String localPart, final String value)
  6. createElement(String namespaceURI, String name)
  7. createElement(String namespaceURI, String name)
  8. createNamespace(Element el, String ns)