Example usage for org.w3c.dom Document getDocumentElement

List of usage examples for org.w3c.dom Document getDocumentElement

Introduction

In this page you can find the example usage for org.w3c.dom Document getDocumentElement.

Prototype

public Element getDocumentElement();

Source Link

Document

This is a convenience attribute that allows direct access to the child node that is the document element of the document.

Usage

From source file:Main.java

/**
 * 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//from w w  w. j a  va2s  .c  o m
 */
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();
}

From source file:Main.java

/**
 * Converts the document from namespace-qualified form into unqualified form by removing all 
 * namespace information.//from ww w .j  a v a 2s.c o  m
 * 
 * @param document the document to convert
 */

public static void convertFromNamespaceForm(final Document document) {
    final Element oldRootElement = document.getDocumentElement();

    if (oldRootElement != null) {
        final Node nodeAfterRootElement = oldRootElement.getNextSibling();
        final Node newRootElement = convertFromNamespaceForm(oldRootElement);
        document.removeChild(oldRootElement);
        document.insertBefore(newRootElement, nodeAfterRootElement);
    }
}

From source file:Main.java

public static void createComment(Document paramDocument, String paramString) {
    Comment localComment = paramDocument.createComment(paramString);
    paramDocument.getDocumentElement().appendChild(localComment);
}

From source file:Main.java

public static Node getNode(String text) throws Exception {
    Node ret = null;//from  w ww .  j a  v a 2s  .c  om

    // Transform the text representation to DOM
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    fact.setNamespaceAware(true);

    java.io.InputStream xmlstr = new java.io.ByteArrayInputStream(text.getBytes());

    DocumentBuilder builder = fact.newDocumentBuilder();
    org.w3c.dom.Document doc = builder.parse(xmlstr);

    xmlstr.close();

    ret = doc.getDocumentElement();

    return (ret);
}

From source file:Main.java

/** Parse a valid xml string and return the Element representing this string. */
public static Element parseXMLString(Document document, String string)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from   www.  j a  va  2s .  c  o  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document subDoc = builder.parse(new InputSource(new StringReader(string)));
    Element e = subDoc.getDocumentElement();
    return (Element) document.importNode(e, true);
}

From source file:Main.java

public static void assertRootElement(Document doc, String rootElementName, String namespaceURI) {
    if (!nodeHasNameNamespace(doc.getDocumentElement(), rootElementName, namespaceURI)) {
        throw new IllegalArgumentException("Error parsing XML document, must have root element with name \""
                + rootElementName + "\", found " + doc.getDocumentElement().getNodeName());
    }/*from   ww w.  j  a v  a2s.  c  o m*/
}

From source file:Main.java

/** This method goes through and adds formatting as necessary so that the XML is human readable
 * TODO work out why we can't put space in front of the first element */
protected static void addFormatting(Document outDoc) {
    // First we want a space before the first element
    addFormatting(outDoc, outDoc.getDocumentElement(), "");

}

From source file:Main.java

/**
 * Compare necessary namespace declarations between original and proposed
 * document, if namespaces in the original are missing compared to the
 * proposed, we add them to the original.
 * //from www .  ja v a 2s . c om
 * @param original document as read from the file system
 * @param proposed document as determined by the JspViewManager
 * @return true if the document was adjusted, otherwise false
 */
private static boolean checkNamespaces(final Document original, final Document proposed) {
    boolean originalDocumentChanged = false;
    final NamedNodeMap nsNodes = proposed.getDocumentElement().getAttributes();
    for (int i = 0; i < nsNodes.getLength(); i++) {
        if (0 == original.getDocumentElement().getAttribute(nsNodes.item(i).getNodeName()).length()) {
            original.getDocumentElement().setAttribute(nsNodes.item(i).getNodeName(),
                    nsNodes.item(i).getNodeValue());
            originalDocumentChanged = true;
        }
    }
    return originalDocumentChanged;
}

From source file:Main.java

/**
 * Clones a document object.//from  w w w  . j  a  v a2 s  .  c  om
 *
 * @param doc The document to be cloned.
 * @return The new document object that contains the same data as the original document.
 * @throws TransformerException Thrown if the document can't be
 */
public static Document cloneDocument(final Document doc) throws TransformerException {
    final Node rootNode = doc.getDocumentElement();

    // Copy the doctype and xml version type data
    final TransformerFactory tfactory = TransformerFactory.newInstance();
    final Transformer tx = tfactory.newTransformer();
    final DOMSource source = new DOMSource(doc);
    final DOMResult result = new DOMResult();
    tx.transform(source, result);

    // Copy the actual content into the new document
    final Document copy = (Document) result.getNode();
    copy.removeChild(copy.getDocumentElement());
    final Node copyRootNode = copy.importNode(rootNode, true);
    copy.appendChild(copyRootNode);

    return copy;
}

From source file:Main.java

public static String toNormalizedXML(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from w ww .ja v a2  s.  c  om
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(is);
    document.normalizeDocument();
    document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
            "xsi:schemaLocation", //$NON-NLS-1$
            "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String xml = serializer.writeToString(document);

    return trim(xml);
}