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

/**
 * Copies all attributes from one element to another in the official way.
 *//*from  w  w w  . j  a  v a  2  s .  com*/
public static void copyAttributes(Element elementFrom, Element elementTo) {
    NamedNodeMap nodeList = elementFrom.getAttributes();
    if (nodeList == null) {
        // No attributes to copy: just return
        return;
    }

    Attr attrFrom = null;
    Attr attrTo = null;

    // Needed as factory to create attrs
    Document documentTo = elementTo.getOwnerDocument();
    int len = nodeList.getLength();

    // Copy each attr by making/setting a new one and
    // adding to the target element.
    for (int i = 0; i < len; i++) {
        attrFrom = (Attr) nodeList.item(i);

        // Create an set value
        attrTo = documentTo.createAttribute(attrFrom.getName());
        attrTo.setValue(attrFrom.getValue());

        // Set in target element
        elementTo.setAttributeNode(attrTo);
    }
}

From source file:es.itecban.deployment.security.client.ws.LogonWS.java

private static void addTextNode(Element element, String nodeName, String textValue) {
    // Get the document
    Document doc = element.getOwnerDocument();
    // Write the name
    Node resourceNameNode = doc.createElement(nodeName);
    element.appendChild(resourceNameNode);
    Text resourceNameText = doc.createTextNode(textValue);
    resourceNameNode.appendChild(resourceNameText);
}

From source file:com.msopentech.odatajclient.engine.data.Deserializer.java

private static XMLLinkCollection toLinkCollectionFromXML(final InputStream input) {
    final Element root = toDOM(input);

    final NodeList uris = root.getOwnerDocument().getElementsByTagName(ODataConstants.ELEM_URI);

    final List<URI> links = new ArrayList<URI>();
    for (int i = 0; i < uris.getLength(); i++) {
        links.add(URI.create(uris.item(i).getTextContent()));
    }/* ww w  .j a  v a 2s  . c  om*/

    final NodeList next = root.getElementsByTagName(ODataConstants.NEXT_LINK_REL);
    final XMLLinkCollection linkCollection = next.getLength() > 0
            ? new XMLLinkCollection(URI.create(next.item(0).getTextContent()))
            : new XMLLinkCollection();
    linkCollection.setLinks(links);

    return linkCollection;
}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Adds a new element to an existing element
 *
 * @param element The parent {@link Element}, which the child will be added to.
 * @param id Tag name of the new {@link Element}
 * @param type xsi type of the new {@link Element}
 * @param value Text value of the new {@link Element}
 * @return//from w w w. j  a va 2  s . c  om
 * @throws SOAPException
 */
public static Element addElement(Element element, String id, String type, String value) throws SOAPException {
    Element child = element.getOwnerDocument().createElement(id);

    if (value != null) {
        child.setTextContent(value);
    }

    SOAPUtil.addTypeAttribute(child, type);

    element.appendChild(child);
    return child;
}

From source file:uk.ac.kcl.Transform23.java

public static void cleanFieldNames(Node node) {
    // do something with the current node instead of System.out
    //System.out.println(node.getNodeName());

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            //calls this method for all the children which is Element
            Element el = (Element) nodeList.item(i);
            //System.out.println(el.getNodeName());
            //System.out.println(nodeList.getLength());
            if (el.getNodeName().contains(".")) {
                el.getOwnerDocument().renameNode(nodeList.item(i), null, el.getNodeName().replace(".", "_"));
            }//from  w w  w .  ja  va  2  s. c  om
            cleanFieldNames(currentNode);
        }
    }
}

From source file:Main.java

/**
 * Set the text of the specified element to the given string.
 * //from   w ww.  ja v  a 2 s  .c om
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

From source file:Main.java

/**
 * Set the text content of an element. All exisitng Text Node are
 * removed before adding a new Text Node containing the given text.
 *
 * @param element target element to set text content, cannot be null.
 * @param text content of the element, cannot be null.
 *///from   w  w  w  .ja v  a2  s .co m
public static void setElementText(Element element, String text) {

    // Remove all text element
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            element.removeChild(n);
        }
    }
    Node child = element.getFirstChild();
    Node textnode = element.getOwnerDocument().createTextNode(text);

    // insert text node as first child
    if (child == null) {
        element.appendChild(textnode);
    } else {
        element.insertBefore(textnode, child);
    }
}

From source file:com.microsoft.tfs.util.xml.DOMUtils.java

/**
 * Adds a new {@link Text} node to a {@link Document} tree as a child of the
 * specified parent {@link Element}.//from  www.  ja  v  a2s. co  m
 *
 * @param parent
 *        the parent {@link Element} (must not be <code>null</code>)
 * @param data
 *        the contents to give the new {@link Text} node
 * @return the new {@link Text} node (never <code>null</code>)
 */
public static Text appendText(final Element parent, final String data) {
    Check.notNull(parent, "parent"); //$NON-NLS-1$

    final Text newChild = parent.getOwnerDocument().createTextNode(data);
    parent.appendChild(newChild);
    return newChild;
}

From source file:com.microsoft.tfs.util.xml.DOMUtils.java

/**
 * Adds a new {@link Element} node to a {@link Document} tree as a child of
 * the specified parent {@link Element}.
 *
 * @param parent/*from  w w w .ja  v a 2 s .c  o  m*/
 *        the parent {@link Element} (must not be <code>null</code>)
 * @param tagName
 *        the name to give the new child {@link Element} (must not be
 *        <code>null</code>)
 * @return the new child {@link Element} (never <code>null</code>)
 */
public static Element appendChild(final Element parent, final String tagName) {
    Check.notNull(parent, "parent"); //$NON-NLS-1$

    final Element newChild = parent.getOwnerDocument().createElement(tagName);
    parent.appendChild(newChild);
    return newChild;
}

From source file:com.microsoft.tfs.util.xml.DOMUtils.java

/**
 * Adds a new {@link CDATASection} node to a {@link Document} tree as a
 * child of the specified parent {@link Element}.
 *
 * @param parent//from w w w. ja  v a  2 s  .com
 *        the parent {@link Element} (must not be <code>null</code>)
 * @param data
 *        the contents to give the new {@link CDATASection} node
 * @return the new {@link CDATASection} node (never <code>null</code>)
 */
public static CDATASection appendCDATA(final Element parent, final String data) {
    Check.notNull(parent, "parent"); //$NON-NLS-1$

    final CDATASection newChild = parent.getOwnerDocument().createCDATASection(data);
    parent.appendChild(newChild);
    return newChild;
}