Example usage for org.w3c.dom Node getOwnerDocument

List of usage examples for org.w3c.dom Node getOwnerDocument

Introduction

In this page you can find the example usage for org.w3c.dom Node getOwnerDocument.

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:Main.java

/**
 * Get the DOM Level 3 Load/Save {@link DOMImplementationLS} for the given node.
 * /*from ww w .jav a 2 s. c  o m*/
 * @param node the node to evaluate
 * @return the DOMImplementationLS for the given node
 */
public static DOMImplementationLS getLSDOMImpl(Node node) {
    DOMImplementation domImpl;
    if (node instanceof Document) {
        domImpl = ((Document) node).getImplementation();
    } else {
        domImpl = node.getOwnerDocument().getImplementation();
    }

    DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
    return domImplLS;
}

From source file:Main.java

/**
   Set the text of a specified node. A text section is added to
   the node if necessary./*from w w w .  ja  v a 2  s. com*/
 */
static public void setText(Node node, String value) {
    if (node == null)
        return;

    Node child = findChild(node, "#text");
    if (child == null)
        node.appendChild(node.getOwnerDocument().createTextNode(value));
    else
        child.setNodeValue(value);
}

From source file:Main.java

/**
 * Add a value to the specified node/*  w w w  .j  ava 2s. co  m*/
 */
public static Node addValue(Node node, String value) {
    final Node newNode;

    if (node instanceof Document)
        newNode = ((Document) node).createTextNode(value);
    else
        newNode = node.getOwnerDocument().createTextNode(value);

    if (newNode != null)
        node.appendChild(newNode);

    return newNode;
}

From source file:Main.java

public static String getExperimentNameFromLineNode(Node linenode) {
    try {/*from w  ww  .  jav  a2 s.  c  o m*/
        Node t1 = linenode.getAttributes().getNamedItem("experimentname");
        if (t1 != null)
            return t1.getNodeValue();
        else
            return linenode.getOwnerDocument().getElementsByTagName("experimentname").item(0).getFirstChild()
                    .getNodeValue();
    } catch (NullPointerException npe) {
        return "";
    }
}

From source file:Main.java

/**
 * Inserts a new child element to a parent.
 * // ww  w . ja va2  s .c o  m
 * @param parent the Element to which to append the child
 * @param name the (tag) name of the new child
 * @param value the text value of the new element. (can be null!)
 * @param pos the inserted element will be placed before this element
 * 
 * @return the new child element
 */
static public Element insertElement(Node parent, String name, String value, Element pos) {
    if (parent == null)
        return null; // Fehler
    Element child = parent.getOwnerDocument().createElement(name);
    if (value != null)
        setElementText(child, value);
    // insert now
    parent.insertBefore(child, pos);
    return child;
}

From source file:Main.java

/**
 * Adds a new empty {@link Element} to the given parent {@link Element}.
 * // w w  w .j a va 2  s .  c o m
 * @param doc the document to add to.
 * @param parent the parent element. If {@code null} then the new child
 * will be added directly to the document.  
 * @param name the name of the new element.
 * @return the created element.
 */
public static Element addElement(Document doc, Node parent, String name) {
    if (doc == null) {
        doc = parent.getOwnerDocument();
    }
    final Element elem = doc.createElement(name);
    if (parent == null) {
        parent = doc;
    }
    parent.appendChild(elem);
    return elem;
}

From source file:Main.java

public static Element addElement(Node parent, String name, Attr[] attrs) {
    Element element;/*from w ww  .  j av a 2s.c om*/
    if (parent instanceof Document) {
        element = ((Document) parent).createElement(name);
    } else {
        element = parent.getOwnerDocument().createElement(name);
    }
    if (attrs != null && attrs.length > 0) {
        for (Attr attr : attrs) {
            element.setAttributeNode(attr);
        }
    }
    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Append text value node./*from  w w  w  .  ja  va2s. co  m*/
 *
 * @param baseNode
 *            the base node
 * @param tagName
 *            the tag name
 * @param tagValue
 *            the tag value
 * @return the element
 */
public static Element appendTextValueNode(Node baseNode, String tagName, String tagValue) {
    Element newNode = appendNode(baseNode, tagName);
    if (tagValue == null) {
        newNode.appendChild(baseNode.getOwnerDocument().createTextNode("<null>"));
    } else {
        newNode.appendChild(baseNode.getOwnerDocument().createTextNode(tagValue));
    }
    return newNode;
}

From source file:Main.java

/**
 * @param parent/*from ww w.  j  a  v  a  2  s.co  m*/
 *          node to add fragment to
 * @param fragment
 *          a well formed XML fragment
 * @throws ParserConfigurationException 
 */
public static void appendXmlFragment(Node parent, String fragment)
        throws IOException, SAXException, ParserConfigurationException {

    DocumentBuilder docBuilder = getBuilder();
    Document doc = parent.getOwnerDocument();

    Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement();

    fragmentNode = doc.importNode(fragmentNode, true);

    parent.appendChild(fragmentNode);
}

From source file:Main.java

/**
 * add single element to a node. it doesn't check if an element exists with the same name.
 * @param contextNode is the node to which the element is added
 * @param elmName is the new element name, not XPath.
 * @param elmValue is the text value of the element.
 * @return the new Element// www . j a v  a 2s.c  om
 */
public static Element addSingleElement(Node contextNode, String elmName, String elmValue) throws Exception {
    Element elm = addSingleElement(contextNode, elmName);
    if (elmValue != null)
        elm.appendChild(contextNode.getOwnerDocument().createTextNode(elmValue));
    return (elm);
}