Example usage for org.w3c.dom Attr getNodeValue

List of usage examples for org.w3c.dom Attr getNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr getNodeValue.

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static String getPrefix(String uri, Node e) {
    while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name;/*ww w.ja  v a  2s  . c  o  m*/
            if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring(6);
            }
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

public static String logElement(Element El, int level) {

    String Es = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//ww w  .  j a v  a2  s. c o m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = El.getNodeName();
    Es = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = El.getAttributes();
    StringBuilder sb = new StringBuilder(Es);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" ");
        // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() +
        // "\" ";
    }
    sb.append(">");
    // Es = Es + ">";
    Es = sb.toString();
    NodeList pl = El.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = DomNode.getNodeValue();
                Es = Es + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        level++;
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) DomNode;
                Es = Es + logElement(el, level);
            }
            i++;
        }
    }
    Es = Es + "\n" + addIndT + "</" + name + ">";

    return Es;
}

From source file:Main.java

/**
 * Convenience method to copy the contents of the old {@link Node} into the
 * new one.//from w  ww . j  a v a 2 s . co m
 * 
 * @param newDoc
 * @param newNode
 * @param oldParent
 */
public static void copyContents(Document newDoc, Node newNode, Node oldNode) {
    // FIXME we should be able to achieve this with much less code (e.g.
    // the code commented out) but for some reason there are
    // incompatibility issues being spat out by the tests.
    //      final NodeList childNodes = oldNode.getChildNodes();
    //        for (int i = 0; i < childNodes.getLength(); i++) {
    //           final Node child = newDoc.importNode(childNodes.item(i), true);
    //           newNode.appendChild(child);
    //        }

    final NodeList childs = oldNode.getChildNodes();

    for (int i = 0; i < childs.getLength(); i++) {
        final Node child = childs.item(i);
        Element newElem = null;
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            //Get all the attributes of an element in a map
            final NamedNodeMap attrs = child.getAttributes();

            // Process each attribute
            newElem = newDoc.createElement(child.getNodeName());
            for (int j = 0; j < attrs.getLength(); j++) {
                final Attr attr = (Attr) attrs.item(j);
                // add attribute name and value to the new element
                newElem.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            newNode.appendChild(newElem);
            break;

        case Node.TEXT_NODE:
            newNode.appendChild(newDoc.createTextNode(getString(child)));
        }
        copyContents(newDoc, newElem, child);
    }
}

From source file:Main.java

public static List<String> getAllAttrValueByName(Node item, String name) {
    List<String> lst = null;
    NamedNodeMap attributes = item.getAttributes();
    int numAttrs = attributes.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        String attrName = attr.getNodeName();
        if (name.equals(attrName)) {
            if (lst == null) {
                lst = new ArrayList();
            }/*w ww. j a v a2s. co m*/
            lst.add(attr.getNodeValue());
        }
    }
    return lst;
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//w w w.j  a  v  a 2  s .c  om
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//from w ww  . j  a  va 2s. c o  m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + place.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

/**
 * Allows to retrieve the UUID of the node.
 * @param node node to be modified./*from   w  w w  . j  a va  2s. c  om*/
 * @return the UUID of the node.
 */
public static String getNodeId(Node node) {
    String nodeId = null;
    NamedNodeMap nnm = node.getAttributes();
    if (nnm != null) {
        Attr a = (Attr) nnm.getNamedItemNS(CEFX_NAMESPACE, CEFXUID);
        if (a == null) {
            String name = CEFXUID;
            a = (Attr) nnm.getNamedItem(name);
        }
        if (a != null) {
            nodeId = a.getNodeValue();
        }
    }
    return nodeId;
}

From source file:Main.java

public static String getAttrvalue(Node item, String name, boolean ignoreNs) {
    NamedNodeMap attributes = item.getAttributes();
    int numAttrs = attributes.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        String attrName = attr.getNodeName();
        String NSName = attr.getNamespaceURI();
        if (ignoreNs) {
            if ((attrName.indexOf(":" + name) != -1) || (name.equals(attrName)))
                return attr.getNodeValue();
        } else {//from w w  w .j  av a2  s.  com
            if (name.equals(attrName)) {
                return attr.getNodeValue();
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node./*w  w  w .ja v  a  2s. co m*/
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start = src;
    Node parent = src;
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) {
    if (node == null) {
        return;//from w  w w  .  j a  va2 s  .co  m
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE:
        string.append("\n");
        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");
        break;

    case Node.ELEMENT_NODE:
        string.append(indentation);
        string.append("<");
        string.append(node.getNodeName());

        Attr[] attributes;
        if (node.getAttributes() != null) {
            int length = node.getAttributes().getLength();
            attributes = new Attr[length];
            for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                attributes[loopIndex] = (Attr) node.getAttributes().item(loopIndex);
            }
        } else {
            attributes = new Attr[0];
        }

        for (Attr attribute : attributes) {
            string.append(" ");
            string.append(attribute.getNodeName());
            string.append("=\"");
            string.append(attribute.getNodeValue());
            string.append("\"");
        }

        string.append(">\n");

        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");

        string.append(indentation);
        string.append("</");
        string.append(node.getNodeName());
        string.append(">\n");

        break;

    case Node.TEXT_NODE:
        string.append(indentation);
        string.append(node.getNodeValue().trim());
        string.append("\n");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        string.append(indentation);
        string.append("<?");
        string.append(node.getNodeName());
        String text = node.getNodeValue();
        if (text != null && text.length() > 0) {
            string.append(text);
        }
        string.append("?>\n");
        break;

    case Node.CDATA_SECTION_NODE:
        string.append(indentation);
        string.append("<![CDATA[");
        string.append(node.getNodeValue());
        string.append("]]>");
        break;
    }
}