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 Element getRootElement(final Node docWithARootElement) {

    if (docWithARootElement != null && docWithARootElement.getChildNodes() != null
            && docWithARootElement.getChildNodes().getLength() > 0) {
        int numOfChildNodes = docWithARootElement.getChildNodes().getLength();

        if (numOfChildNodes == 1) {
            return (Element) docWithARootElement.getFirstChild();
        } else {/*from   w  w  w.ja va2  s . c om*/
            for (int i = 0; i < numOfChildNodes; i++) {
                Node nodeToInspect = docWithARootElement.getChildNodes().item(i);

                if (nodeToInspect != null && nodeToInspect.getNodeType() == Node.ELEMENT_NODE) {
                    return (Element) nodeToInspect;
                }
            }
        }
    }

    return null;

}

From source file:ee.ria.xroad.proxyui.WSDLParser.java

private static String getValue(Node node) {
    if (node.hasChildNodes()) {
        String value = node.getFirstChild().getNodeValue();

        if (value != null) {
            value = value.trim();/*  w w  w .  ja  v a  2  s.c  o  m*/
        }

        return value;
    }

    return null;
}

From source file:org.opencastproject.remotetest.util.Utils.java

/**
 * Converts a node list to a list of their string values. Nodes that do not have a string value are returned as the
 * empty string.//from   w  w w  .ja v  a2  s .  co  m
 */
public static List<String> nodeListToStringList(NodeList nodes) {
    List<String> strings = new ArrayList<String>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeValue() != null) {
            strings.add(node.getNodeValue().trim());
        } else {
            Node fchild = node.getFirstChild();
            if (fchild != null && fchild.getNodeValue() != null) {
                strings.add(fchild.getNodeValue().trim());
            } else {
                strings.add("");
            }
        }

    }
    return strings;
}

From source file:Main.java

/**
 * This method creates a collection of child nodes from a parent node
 * @param root the parent node// w  ww  . j  a  v a  2 s .c o  m
 * @return Collection - childern nodes
 */
public static Collection getCollection(Node root) {
    Collection collection = new ArrayList();
    if (root != null) {
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                if (child.getChildNodes().getLength() > 1) {
                    collection.add(child);
                } else {
                    Node textChild = child.getFirstChild();
                    if (textChild == null) {
                        // don't accept nulls
                    } else {
                        collection.add(child.getFirstChild().getNodeValue());
                    }
                }
            }
        }
    }
    return collection;
}

From source file:Main.java

/**
 * This method creates a map of all of the childern of a node
 * @param root - Parent node//from   w  ww  .  j  av a 2 s . c om
 * @return HashMap key value map of the children of the parent node
 */
public static HashMap getMap(Node root) {
    HashMap map = new HashMap();
    if (root != null) {
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                if (child.getChildNodes().getLength() > 1) {
                    map.put(nodeName, child);
                } else {
                    Node textChild = child.getFirstChild();
                    if (textChild == null) {
                        map.put(nodeName, null);
                    } else {
                        map.put(nodeName, child.getFirstChild().getNodeValue());
                    }
                }
            }
        }
    }
    return map;
}

From source file:com.karlchu.mo.web.sitemesh.MyConfigLoader.java

private static String getContainedText(Node parent, String childTagName) {
    try {//from  ww w  .  ja v  a 2 s.  c om
        Node tag = ((Element) parent).getElementsByTagName(childTagName).item(0);
        return ((Text) tag.getFirstChild()).getData();
    } catch (Exception e) {
        return null;
    }
}

From source file:eu.smartfp7.terrier.sensor.ParserUtility.java

public static EdgeNodeSnapShot parse(InputStream is) throws Exception {
    DocumentBuilderFactory xmlfact = DocumentBuilderFactory.newInstance();
    xmlfact.setNamespaceAware(true);//www .  j a  v a2s  .c  o  m
    Document document = xmlfact.newDocumentBuilder().parse(is);

    NamespaceContext ctx = new NamespaceContext() {

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getNamespaceURI(String prefix) {

            String uri = null;

            /*
             *    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:smart="http://www.ait.gr/ait_web_site/faculty/apne/schema.xml#"
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:contact="http://www.w3.org/2000/10/swap/pim/contact#"
            xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
            xmlns:time="http://www.w3.org/2006/time#"
            xml:base="http://www.ait.gr/ait_web_site/faculty/apne/report.xml"
                    
            *
            */

            if (prefix.equals("rdf")) {
                uri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
            }
            if (prefix.equals("smart")) {
                uri = "http://www.ait.gr/ait_web_site/faculty/apne/schema.xml#";
            }
            if (prefix.equals("dc")) {
                uri = "http://purl.org/dc/elements/1.1/#";
            }
            if (prefix.equals("geo")) {
                uri = "http://www.w3.org/2003/01/geo/wgs84_pos#";
            }
            if (prefix.equals("time")) {
                uri = "http://www.w3.org/2006/time#";
            }
            return uri;

        }
    };

    // find the node data

    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(ctx);

    String id = (String) xpath.compile("//smart:Node/@rdf:ID").evaluate(document, XPathConstants.STRING);
    String lat = (String) xpath.compile("//smart:Node/geo:lat/text()").evaluate(document,
            XPathConstants.STRING);
    String lon = (String) xpath.compile("//smart:Node/geo:long/text()").evaluate(document,
            XPathConstants.STRING);
    String fullName = (String) xpath.compile("//smart:Node/dc:fullName/text()").evaluate(document,
            XPathConstants.STRING);
    Node node = new Node(new Double(lat), new Double(lon), id, fullName);

    String time = (String) xpath.compile("//smart:Report/time:inXSDDateTime/text()").evaluate(document,
            XPathConstants.STRING);
    String reportId = (String) xpath.compile("//smart:Report/@rdf:ID").evaluate(document,
            XPathConstants.STRING);

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Calendar c = Calendar.getInstance();
    ;
    c.setTime(df.parse(time));

    String density = (String) xpath.compile("//smart:Crowd/smart:density/text()").evaluate(document,
            XPathConstants.STRING);
    String cameraGain = (String) xpath.compile("//smart:Crowd/smart:cameraGain/text()").evaluate(document,
            XPathConstants.STRING);

    NodeList list = (NodeList) xpath.compile("//smart:Crowd/smart:colour").evaluate(document,
            XPathConstants.NODESET);
    double[] colors = new double[list.getLength()];
    for (int i = 0; i < list.getLength(); i++) {
        org.w3c.dom.Node colorNode = list.item(i);
        String v = colorNode.getFirstChild().getTextContent();
        colors[i] = new Double(v);
    }

    CrowdReport crowdReport = new CrowdReport(reportId, new Double(density), new Double(cameraGain), colors);

    EdgeNodeSnapShot snapShot = new EdgeNodeSnapShot(node, id, c, crowdReport);

    return snapShot;
}

From source file:com.betfair.testing.utils.cougar.assertions.AssertionUtils.java

private static void doDomSorting(Document doc, XPath xpath, String x)
        throws XPathExpressionException, IOException {
    NodeList parentNodes = (NodeList) xpath.evaluate(x, doc, XPathConstants.NODESET);
    for (int i = 0; i < parentNodes.getLength(); i++) {
        Node n = parentNodes.item(i);
        List<Node> allKids = new ArrayList<>(n.getChildNodes().getLength());
        for (int j = n.getChildNodes().getLength() - 1; j >= 0; j--) {
            allKids.add(n.removeChild(n.getFirstChild()));
        }// ww w.  j  ava2 s  .c  o m
        final Map<Node, String> kidsToString = new HashMap<>();

        for (Node k : allKids) {
            kidsToString.put(k, toString(k));
        }
        Collections.sort(allKids, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return kidsToString.get(o1).compareTo(kidsToString.get(o2));
            }
        });
        for (Node k : allKids) {
            n.appendChild(k);
        }
    }
}

From source file:Main.java

/**
 * Returns String representation of specified xml node.
 *
 * @param node   the specified xml node/*  w  ww.j  a  v  a2s  . c  o m*/
 * @return string representation of the node
 */
public static String getNodeData(Node node) {
    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())*/
        Node child = node.getFirstChild();
        if (child != null)
            return getNodeData(child);
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    case Node.ATTRIBUTE_NODE:
        return node.getNodeValue();
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        break;
    }
    return "";
}

From source file:Main.java

public static void collectXpathContainText(Node node, String textContent, List<String> holder) {
    if (textContent.equals(node.getTextContent())) {
        String xpath = getXPath(node);
        if (!holder.contains(xpath)) {
            holder.add(xpath);//from  www  .j a  va 2 s.  c o m
        }
    }

    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            collectXpathContainText(child, textContent, holder);
            child = child.getNextSibling();
        }
    }
}