Example usage for org.w3c.dom Node ATTRIBUTE_NODE

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

Introduction

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

Prototype

short ATTRIBUTE_NODE

To view the source code for org.w3c.dom Node ATTRIBUTE_NODE.

Click Source Link

Document

The node is an Attr.

Usage

From source file:Main.java

public static boolean isAttribute(Node node) {
    return node.getNodeType() == Node.ATTRIBUTE_NODE;
}

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }/*from   w  w w  .  j ava 2s. c  o  m*/
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

public static List<Attr> getAttributes(Element element) {

    List<Attr> attributes = new LinkedList<Attr>();

    NamedNodeMap namedNodeMap = element.getAttributes();

    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node node = namedNodeMap.item(i);
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            Attr a = (Attr) node;
            attributes.add(a);/*from  ww w  .  ja  v  a 2 s . c  om*/
        }
    }

    return attributes;
}

From source file:Main.java

/**
 * Convenience method for setting a node's (attr or elem) text value.
 * /*from  w  ww. ja v a  2 s  .c o  m*/
 * @param node     The DOM node whose value needs to be changed.
 * @param newValue The value to set for node.
 */
public static void setNodeValue(final Node node, final String newValue) {
    short nodeType = node.getNodeType();
    if (nodeType == Node.ATTRIBUTE_NODE) {
        node.setNodeValue(newValue);
    } else if (nodeType == Node.ELEMENT_NODE) {
        node.setTextContent(newValue);
    }
}

From source file:Main.java

public static void removeAllSubNodesExceptAttributes(Node n) {
    NodeList childNodes = n.getChildNodes();
    List<Node> childNodesToRemove = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node c = childNodes.item(i);

        if (c.getNodeType() != Node.ATTRIBUTE_NODE) {
            childNodesToRemove.add(c);/*  w ww .j  av  a  2  s. com*/
        }
    }

    for (Node c : childNodesToRemove) {
        n.removeChild(c);
    }
}

From source file:Main.java

/**
 * Prints a textual representation of the given node to the specified PrintStream.
 *
 * @param  n    Node that is to be printed.
 * @param  out  The PrintStream to which the node is to be printed.
 * @pre    n != null && out != null
 */// w w w  . j  a v a2s .  c o  m
public static void printXMLNode(Node n, PrintStream out) {
    switch (n.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.println("DOC_ROOT");
        break;

    case Node.ELEMENT_NODE:
        out.println("<" + ((Element) n).getTagName() + ">");
        break;

    case Node.ATTRIBUTE_NODE:
        out.println("@" + ((Attr) n).getName());
        break;

    case Node.TEXT_NODE:
        out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
        break;

    case Node.COMMENT_NODE:
        out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
        break;

    default:
        out.println("Unknown node type: " + n.getNodeType());
    }
}

From source file:Main.java

public static String getTextContent(final Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:// www.  j  a v  a  2s .  c  om
        return null;
    }
}

From source file:Main.java

/**
 * Returns true if the descendantOrSelf is on the descendant-or-self axis
 * of the context node./*from   w w  w  .  ja  va  2  s  .co m*/
 *
 * @param ctx
 * @param descendantOrSelf
 * @return true if the node is descendant
 */
static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) {

    if (ctx == descendantOrSelf) {
        return true;
    }

    Node parent = descendantOrSelf;

    while (true) {
        if (parent == null) {
            return false;
        }

        if (parent == ctx) {
            return true;
        }

        if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
            parent = ((Attr) parent).getOwnerElement();
        } else {
            parent = parent.getParentNode();
        }
    }
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }//from w ww . j a va  2s  . c  om
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:XmlUtil.java

/**
 * Returns a map of all node's attributes. All non-attribute nodes are ignored.
 *//*from   ww  w  . j a v  a  2  s  .co m*/
public static Map<String, String> getAllAttributes(Node node) {
    HashMap<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
        Node attribute = nmm.item(j);
        if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }
        attrs.put(attribute.getNodeName(), attribute.getNodeValue());
    }
    return attrs;
}