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

/**
 * Method getStrFromNode/*from  ww  w . ja va  2 s .  c om*/
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode()
                .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return xpathnode.getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return xpathnode.getNodeValue();
    }

    return null;
}

From source file:Main.java

/**
 * Returns the node type name from the node type code
 *
 * @param nodeType//from  ww w .j av a  2 s.co m
 * @return
 */
public static String getTypeName(short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return "element";
    case Node.DOCUMENT_NODE:
        return "document";
    case Node.TEXT_NODE:
        return "text";
    case Node.ATTRIBUTE_NODE:
        return "attribute";
    case Node.CDATA_SECTION_NODE:
        return "cdata";
    }
    return "Unknown[" + nodeType + "]";
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;// ww  w  .  j  a v  a 2s  . co m
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

public static String getAttrValue(NamedNodeMap attrs, String attrName) {
    if (attrs == null || attrName == null) {
        return null;
    }//from   w  w w .  j  a va  2s .c om
    String attrValue = null;

    Node attrNode = attrs.getNamedItem(attrName);
    if (attrNode == null || Node.ATTRIBUTE_NODE != attrNode.getNodeType()) {
        return null;
    }

    attrValue = attrNode.getNodeValue();
    return attrValue;
}

From source file:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default:/*from   ww w . j  av  a 2 s  .co  m*/
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}

From source file:Main.java

/**
 * Prints information of the specified node.
 * //from  w w w .jav a2  s  .c  o m
 * @param node the specified node.
 * @param level a number of indent.
 */
public static void printNode(Node node, int level) {
    if (node == null)
        return;
    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        printAttribute(node, level);
        break;
    case Node.TEXT_NODE:
        printTextNode(node, level);
        break;
    default:
        printElement(node, level);
    }
}

From source file:Main.java

protected static Map<String, String> createAttributeTable(Element e) {
    Map<String, String> attributeTable = new HashMap<>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int numAttrs = attrs.getLength();
        for (int i = 0; i < numAttrs; i++) {
            Node na = attrs.item(i);
            if (na.getNodeType() != Node.ATTRIBUTE_NODE) {
                continue;
            }// w w  w. ja  va2s . c o m
            Attr a = (Attr) na;
            attributeTable.put(a.getName(), a.getValue());
        }
    }
    return attributeTable;
}

From source file:Main.java

/**
 * Gets Node type String for a given node type constant
 *///from   w w w  .  j a va 2s.  c  o  m

public static String getNodeTypeStr(int nodeType)

{

    switch (nodeType)

    {

    case Node.ATTRIBUTE_NODE:

        return "ATTRIBUTE_NODE ";

    case Node.CDATA_SECTION_NODE:

        return "CDATA_SECTION_NODE";

    case Node.COMMENT_NODE:

        return "COMMENT_NODE";

    case Node.DOCUMENT_FRAGMENT_NODE:

        return "DOCUMENT_FRAGMENT_NODE";

    case Node.DOCUMENT_TYPE_NODE:

        return "DOCUMENT_TYPE_NODE";

    case Node.ELEMENT_NODE:

        return "ELEMENT_NODE";

    case Node.ENTITY_NODE:

        return "ENTITY_NODE";

    case Node.ENTITY_REFERENCE_NODE:

        return "ENTITY_REFERENCE_NODE";

    case Node.NOTATION_NODE:

        return "NOTATION_NODE";

    case Node.PROCESSING_INSTRUCTION_NODE:

        return "PROCESSING_INSTRUCTION_NODE";

    case Node.TEXT_NODE:

        return "TEXT_NODE";

    case Node.DOCUMENT_NODE:

        return "DOCUMENT_NODE";

    default:

        return "UN-INDENTIFIED NODE";

    }

}

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 . j  a  v  a 2s  . com*/
 *
 * @param ctx
 * @param descendantOrSelf
 * @return true if the node is descendant
 */
public static 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:Main.java

private 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:/*from  w w  w  .j a va 2 s .  co  m*/
        return null;
    }
}