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

/**
 * Get the first child element with a specified name.
 * If the starting node is a Document, use the document element
 * as the starting point. Only first-generation children of the
 * starting node are searched./*from   w w  w  .  ja  v  a  2s  . c o  m*/
 * @param node the starting node.
 * @param name the name of the child to find.
 * @return the first child element with the specified name, or null
 * if the starting node is null or if no child with the name exists.
 */
public static Element getFirstNamedChild(Node node, String name) {
    if (node == null)
        return null;
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return null;
    Node child = node.getFirstChild();
    while (child != null) {
        if ((child instanceof Element) && child.getNodeName().equals(name)) {
            return (Element) child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:net.openkoncept.vroom.VroomUtilities.java

/**
 * <p>/*from   w  ww.java 2 s.c  om*/
 * This method is used to return the content of an XMl element.
 * </p>
 *
 * @param node - XML element
 * @return - the content or empty string in case of exception.
 */
public static String getXmlText(Node node) {
    try {
        String value = node.getFirstChild().getNodeValue().trim(); // node.getTextContent();
        if (value == null || value.length() == 0) {
            value = node.getFirstChild().getNextSibling().getNodeValue().trim();
        }
        return value;
    } catch (Exception ex) {
        return "";
    }
}

From source file:com.buzzdavidson.spork.util.XmlUtils.java

/**
 * Descends from the given parent looking for the first node to match path.
 * @param parent The DOM parent from which to start.
 * @param path The set of regular expressions for each level of the descent.
 * @return The child node at the end of the path or null if no match.
 *///from w w  w  .j  a v a 2s .  c  o m
public static Node getChild(Node parent, Pattern[] path) {
    Node c = parent;
    int i;

    for (i = 0; c != null && i < path.length; ++i) {
        logger.debug("Looking for node matching [" + path[i].pattern() + "]");
        for (c = c.getFirstChild(); c != null; c = c.getNextSibling()) {
            String nn = c.getNodeName();
            Matcher m = path[i].matcher(nn);
            //logger.info("Checking node: " + nn);
            if (m != null && m.matches()) {
                logger.debug("Found matching node: " + nn);
                break;
            }
        }
    }

    return (i == path.length) ? c : null;
}

From source file:Main.java

/**
 * For the given {@code sourceNode}, read each "top level" element into the resulting {@code Map}. Each element name
 * is a key to the map, each element value is the value paired to the key. Example - anchor is the node, label and
 * href are keys://from  ww  w . ja  v  a 2  s  .  c  o  m
 *
 * <pre>
 * {@code
 * <anchor>
 *  <label>Slashdot</label>
 *    <href>http://slashdot.org/</href>
 *  </anchor>
 * }
 * </pre>
 */
public static Map<String, String> readNodeElementsToMap(final Node sourceNode) {
    Map<String, String> result = new HashMap<String, String>();

    if (sourceNode == null) {
        return result;
    }

    NodeList childNodes = sourceNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node element = childNodes.item(i);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            String elementName = element.getNodeName();
            String elementValue = "";
            Node firstChild = element.getFirstChild();

            if (firstChild != null) {
                elementValue = firstChild.getNodeValue();
            }

            if (elementValue != null) {
                result.put(elementName, elementValue);
            }
        }
    }

    return result;
}

From source file:Main.java

/**
 * @param fontNode//from  ww w. j av  a  2s.  co m
 * @return the style
 */
public static String toFontStyle(Node fontNode) {
    // bold not supported in SLD?
    final String[] styles = { "normal", "normal", "italic" };

    String value = styles[0];
    try {// FIXME no good to grab 1st child than node val
        String nodeVal = fontNode.getFirstChild().getNodeValue();
        String[] components = nodeVal.split(", ");

        // cheap, cheap, cheap
        value = styles[Integer.parseInt(components[1])];
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static String readProjectName(String file) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    InputStream is = new FileInputStream(file);
    Document doc = dombuilder.parse(is);
    Element root = doc.getDocumentElement();
    NodeList prjInfo = root.getChildNodes();
    if (prjInfo != null) {
        for (int i = 0; i < prjInfo.getLength(); i++) {
            Node project = prjInfo.item(i);
            if (project.getNodeType() == Node.ELEMENT_NODE) {
                String strProject = project.getNodeName();
                if (strProject.equals("project")) {
                    for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            String strNodeName = node.getNodeName();
                            if (strNodeName.equals("name")) {
                                return node.getTextContent();
                            }// www . j  av  a2s.  c o  m
                        }
                    }
                }
            }

        }
    }
    return "";
}

From source file:Main.java

public static String getTextContent(Node e) {
    if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
        return null;
    }//  w w  w. j ava2 s . c om

    NodeList nodes = e.getChildNodes();
    StringBuilder text = new StringBuilder();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = e.getFirstChild();

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String s = node.getNodeValue();
            if (s != null) {
                text.append(s);
            }
        }
    }

    if (text.length() > 0) {
        return text.toString();
    } else {
        return null;
    }
}

From source file:Main.java

public static void getNodeData(Node node, StringBuffer buf) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_NODE:
    case Node.ELEMENT_NODE: {
        for (Node child = node.getFirstChild(); null != child; child = child.getNextSibling()) {
            buf.append('<');
            buf.append(node.getNodeName());
            buf.append('>');
            getNodeData(child, buf);/*w w w  .  j  av a 2 s.  c o m*/
            buf.append("</");
            buf.append(node.getNodeName());
            buf.append('>');
        }
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.ATTRIBUTE_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
        break;
    default:
        // ignore
        break;
    }
}

From source file:XMLUtils.java

public static void removeContents(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        parent.removeChild(node);//from w  ww. j  a v  a  2  s.c om
        node = node.getNextSibling();
    }
}

From source file:DomUtil.java

/**
 * Get the first element child./*from  w  ww .  ja  v  a  2  s  .  c  o  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;
}