Example usage for org.w3c.dom Node getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

public static Object toObject(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        node = node.getFirstChild();
        Map<String, Object> map = new LinkedHashMap<>(1);
        map.put(node.getNodeName(), toObject(node));
        return map;
    }/*from w  w w .  ja  v  a 2 s  . c o m*/
    Object value = getElementValue(node);
    if (node.hasAttributes()) {
        Map<String, Object> wrapper = new LinkedHashMap<>(2);
        wrapper.put("_", value);
        wrapper.put("@", getAttributes(node));
        return wrapper;
    } else {
        return value;
    }
}

From source file:Main.java

/**
 * Returns a list of children with the specified name
 * @param name//from ww  w  .ja  v a  2 s .c om
 * @return
 */
public static ArrayList<Node> getChildrenByTagName(Node node, String name) {
    ArrayList<Node> list = new ArrayList<Node>();
    if (node == null)
        return list;

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeName().equals(name))
            list.add(child);
    }

    return list;
}

From source file:Utils.java

/**
 * Get the first element child.// w w  w  .j  a  v  a 2 s.co m
 * 
 * @param parent lookup direct childs
 * @param name name of the element. If null return the first element.
 */
public static Node getChild(Node parent, String name) {
    if (parent == null) {
        return null;
    }

    Node first = parent.getFirstChild();
    if (first == null) {
        return null;
    }

    for (Node node = first; node != null; node = node.getNextSibling()) {
        // System.out.println("getNode: " + name + " " +
        // node.getNodeName());
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (name != null && name.equals(node.getNodeName())) {
            return node;
        }
        if (name == null) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

public static String getText(Node elem) {
    if (elem.getNodeType() == Element.TEXT_NODE)
        return elem.getTextContent();
    else if (elem.hasChildNodes())
        return elem.getFirstChild().getTextContent();
    return "";
}

From source file:Main.java

static public Element findFirstChildElementNS(Node parent, String namespaceuri, String qname) {
    if (parent == null)
        return null;
    return findChildElementNS(parent.getFirstChild(), null, namespaceuri, qname);
}

From source file:Main.java

private static int findNodeIndex(Node node) {
    String nm = node.getLocalName();
    String ns = node.getNamespaceURI();
    short nt = node.getNodeType();

    Node parentNode = node.getParentNode();
    if (parentNode.getNodeType() != Node.ELEMENT_NODE)
        return 1;

    Node child = parentNode.getFirstChild();

    int ix = 0;//from  w  w  w.ja v  a 2 s  . co m
    while (child != null) {
        if (child == node)
            return ix + 1;

        if (child.getNodeType() == nt && nm.equals(child.getLocalName())
                && ((ns == null && child.getNamespaceURI() == null)
                        || (ns != null && ns.equals(child.getNamespaceURI()))))
            ix++;

        child = child.getNextSibling();
    }

    throw new RuntimeException("Child node not found in parent!?");
}

From source file:Main.java

/**
 * Get sub tag content.//from  w  ww  . j  ava 2s . c o m
 * @param node to process
 * @param tagName the tag name
 * @return the child content
 */
public static String getSubTagValue(Node node, String tagName) {
    if (node == null) {
        return null;
    }
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n instanceof Element) {
            if (((Element) n).getTagName().equals(tagName)) {
                Node nn = n.getFirstChild();
                if (nn instanceof Text) {
                    return ((Text) nn).getData();
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static List childNodeList(Node node, String childNodeName) {
    if (node == null)
        return null;

    List children = new LinkedList();
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {/*from w  w  w  . ja  va 2s  .co m*/
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                children.add(childNode);
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return children;
}

From source file:Main.java

/**
 * @param colorNode/*  w  ww  . j av a2  s. c  o  m*/
 * @return the family name
 */
public static String toFontFamily(Node colorNode) {
    String value = "Dialog";

    try {// FIXME no good to grab 1st child than node val
        String nodeVal = colorNode.getFirstChild().getNodeValue();
        String[] components = nodeVal.split(", ");
        value = components[0];
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes, then collects and returns
 * the first match node's value. Useful when caller knows that there is only
 * one child node.//www .ja v a 2 s.c  o  m
 *
 * @param node     The parent node
 * @param nodeName The child node element name
 * @return The child node's value
 */
public static String getNodeValue(Node node, String nodeName) {
    Node n = getNode(node, nodeName);
    if (n == null) {
        return null;
    }
    Node ch = n.getFirstChild();
    if (ch != null) {
        return ch.getNodeValue();
    }
    return null;

}