Example usage for org.w3c.dom Element getOwnerDocument

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

Introduction

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

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:Main.java

/**
 * Adds an {@link Element} child to the given element.
 * <p>//from  w  ww  .  j  ava 2s. c  o m
 * If you only want to generate elements for a document that is to be
 * serialized, this method is fine.  If, however, you want to manipulate
 * the document, you should probably use
 * {@link #addElementChildTo(Element,String,String)}.
 *
 * @param parent The {@link Element} to add the {@link Element} child to.
 * @param tagName The tag name of the new {@link Element} child.
 * @return Returns A new {@link Element} with its <code>nodeName</code> set
 * to <code>tagName</code>, and <code>localName</code>, <code>prefix</code>,
 * and <code>namespaceURI</code> set to <code>null</code>.
 * @see #addElementChildTo(Element,String,String)
 */
public static Element addElementChildTo(Element parent, String tagName) {
    final Document doc = parent.getOwnerDocument();
    final Element child = doc.createElement(tagName);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static String elementToString(Element el) {
    if (el == null)
        return "";
    Document document = el.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    return serializer.writeToString(el);
}

From source file:Main.java

public static Element createElement(Element parent, String name) {
    Document document;//from w ww  .j  a v  a2 s  . com
    Element element;

    document = parent.getOwnerDocument();
    element = document.createElement(name);

    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Add literal text to the supplied element.
 * @param element Target DOM Element./*from ww w  .java2s.  c  o m*/
 * @param literalText Literal text to be added.
 * @return if true all the operation are done. 
 */
public static boolean addLiteral(Element element, String literalText) {
    try {
        Document document = element.getOwnerDocument();
        Text literal = document.createTextNode(literalText);
        element.appendChild(literal);
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Create an element with the specified name
 *
 * @param parent/*  w w w .  j  a  v a 2  s . c  om*/
 * @param tagName
 * @return
 */
public static Element createElement(Element parent, String tagName) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static void addNamespaceMapping(Element importsElement, String dictionaryPrefix, String dictionaryUri) {
    Element importElement = importsElement.getOwnerDocument().createElement("import");
    importElement.setAttribute("uri", dictionaryUri);
    importElement.setAttribute("prefix", dictionaryPrefix);
    importsElement.appendChild(importElement);
}

From source file:Main.java

public static void write(Element root, String xmlPath) {
    try {//w w  w  . j a  v  a 2s  .  c  om
        Document doc = root.getOwnerDocument();
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        FileWriter fw = new FileWriter(xmlPath);

        transformer.transform(new DOMSource(doc), new StreamResult(fw));

        fw.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Appends the child element as well as value to the parent element.
 *
 * @param parent the parent element/*from www .j  a v a2s .com*/
 * @param tagName the child element name
 * @param value the child element value
 * @return the child element added to the parent element
 */
public static Element appendElement(Element parent, String tagName, String value) {
    Element child = appendElement(parent, tagName);
    child.appendChild(child.getOwnerDocument().createTextNode(value));
    return child;
}

From source file:Main.java

/**
 * Creates a DOM element//from w w  w  .j a va  2s.  c  o  m
 * @param parent parent DOM element
 * @param tagName element name
 * @return a DOM element
 */
public static Element createElement(Element parent, String tagName) {
    Document doc = parent.getOwnerDocument();
    Element e = doc.createElement(tagName);
    parent.appendChild(e);
    return e;
}

From source file:Main.java

/**
 * Append XPath [expr] like /a/b[1] or /a/b[@name='x']
 * @param sb/*w  ww . j  av  a2s  .c om*/
 * @param n
 */
private static void appendElementQualifier(StringBuffer sb, Element n) {
    if (n.getParentNode() == n.getOwnerDocument()) {
        return;
    }

    if (n.getAttributes() != null && n.getAttributes().getNamedItem("name") != null) {
        sb.append("[@name='").append(n.getAttributes().getNamedItem("name").getNodeValue()).append("']");
    } else if (getChildrenCount((Element) n.getParentNode(), n.getNamespaceURI(), n.getLocalName()) != 1) {
        sb.append("[").append(getPosition(n)).append("]");
    }
}