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:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static Node findFirstDescendantHasAttr(Node parent, String attrName, String attrValue) {
    if (parent == null)
        return null;
    Node n = parent.getFirstChild();
    while (n != null) {
        NamedNodeMap attrs = n.getAttributes();
        if (attrs != null) {
            Node attr = attrs.getNamedItem(attrName);
            if (attr != null) {
                if (attr.getNodeValue().equals(attrValue)) {
                    return n;
                }// ww  w .j ava  2  s  .  c  o  m
            }
        }
        Node d = findFirstDescendantHasAttr(n, attrName, attrValue);
        if (d != null) {
            return d;
        }
        n = n.getNextSibling();
    }
    return n;
}

From source file:Main.java

/**
 *  Gets the node value as date.//from  ww w  .ja v a2 s.  c o m
 *
 *@param  node                          Description of the Parameter
 *@return                               The nodeValueAsDate value
 *@exception  DOMException              Description of the Exception
 *@exception  ParseException  Description of the Exception
 */
public final static Date getNodeValueAsDate(Node node) throws DOMException, ParseException {
    if (node == null)
        return null;

    NamedNodeMap attrs = node.getAttributes();
    Node attr = attrs.getNamedItem("DateTimeFormat");

    // Date format
    String format = attr.getNodeValue().trim();
    node = node.getFirstChild();
    if (node != null) {
        String date = node.getNodeValue().trim();
        DateFormat df = new SimpleDateFormat(format);
        return df.parse(date);
    }
    return null;
}

From source file:Main.java

protected static void getTextFromNode(Node node, StringBuffer buffer, boolean addSpace) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        buffer.append(node.getNodeValue());
        if (addSpace)
            buffer.append(" ");
    }/*  ww  w.  j  a  va  2 s .  c  o  m*/
    Node child = node.getFirstChild();
    while (child != null) {
        getTextFromNode(child, buffer, addSpace);
        child = child.getNextSibling();
    }
}

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

public static EdgeNodeSnapShot parseShort(InputStream is) throws Exception {

    DocumentBuilderFactory xmlfact = DocumentBuilderFactory.newInstance();
    xmlfact.setNamespaceAware(true);//www.  j a va2s.c  o  m
    Document document = xmlfact.newDocumentBuilder().parse(is);

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

    String time = (String) xpath.compile("//crowd/time/text()").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("//crowd/density/text()").evaluate(document, XPathConstants.STRING);

    NodeList list = (NodeList) xpath.compile("//crowd/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);
    }

    String activity = (String) xpath.compile("//activity/name/text()").evaluate(document,
            XPathConstants.STRING);

    CrowdReport crowdReport = new CrowdReport(null, new Double(density), 0.0, colors);

    EdgeNodeSnapShot snapShot = new EdgeNodeSnapShot(null, null, c, crowdReport);
    snapShot.setText((activity != null ? activity : StringUtils.EMPTY));

    return snapShot;

}

From source file:ProcessorDemo.java

private static void readConfig(String confFile) {
    String parent = new File(confFile).getParentFile().getParent();
    try {//from ww w. j ava  2s  .  c o  m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(confFile));
        NodeList nl = doc.getFirstChild().getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            org.w3c.dom.Node n = nl.item(i);
            if (n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                String nn = n.getNodeName();
                String value = n.getFirstChild().getNodeValue();

                if (nn.equals("composit")) {
                    compositRule += value + "\n";
                }
                if (nn.equals("compound")) {
                    if (value.equals("\u69cb\u6210\u8a9e")) {
                        isCompound = false;
                    }
                }
                if (nn.equals("remark")) {
                    remarkRule += value + "\n";
                }
                if (nn.equals("dictionary")) {
                    // read nested tag in <dictinary>
                    NodeList dnl = n.getChildNodes();
                    for (int j = 0; j < dnl.getLength(); j++) {
                        org.w3c.dom.Node dn = dnl.item(j);
                        if (dn.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {

                            String dnn = dn.getNodeName();
                            if (dn.getFirstChild() == null) {
                                throw new IllegalArgumentException("element '" + dnn + "' is empty");
                            }
                            String dvalue = dn.getFirstChild().getNodeValue();

                            if (dnn.equals("compound")) {
                                compoundFile = SenUtils.getPath(dvalue, parent);
                            }
                        }
                    }
                }
            }
        }
        if (!isCompound) {
            try {
                ObjectInputStream is = new ObjectInputStream(new FileInputStream(compoundFile));
                HashMap hashmap = (HashMap) is.readObject();
            } catch (ClassNotFoundException e1) {
                throw new RuntimeException(e1);
            }
        }
    } catch (ParserConfigurationException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (FileNotFoundException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (SAXException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (IOException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
}

From source file:com.isa.ws.utiles.UtilesSWHelper.java

public static String getNodeValue(String xml, String node) {
    //get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {/*  w  ww  . j a  v a 2 s .c  o m*/

        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();

        //parse using builder to get DOM representation of the XML file
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = db.parse(is);
        NodeList nodelist = dom.getElementsByTagName(node);
        Node node1 = nodelist.item(0);
        String value = null;
        if (node1.getFirstChild() != null) {
            if (node.equals("css:validity")) {
                value = "";
                value += node1.getChildNodes().item(0).getFirstChild().getNodeValue();
                value += ",";
                value += node1.getChildNodes().item(1).getFirstChild().getNodeValue();
            } else
                value = node1.getFirstChild().getNodeValue();
        }
        return value;

    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
        return null;
    } catch (SAXException se) {
        se.printStackTrace();
        return null;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static <T> Map<String, T> loadBeans(Node parent, Class<T> beanClass)
        throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        DOMException, InvocationTargetException {
    Map<String, T> store = new HashMap<String, T>();

    Node node = parent.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String id = getId(node);
            T bean = loadBean(node, beanClass);
            store.put(id, bean);//from  www.java 2 s  . co  m
        }
        node = node.getNextSibling();
    }

    return store;
}

From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

public static <T> Trio<Collection<RpcHeader>, RpcFault, T> parseSoapResponse(Class<T> returnType,
        String methodName, InputStream is, Converter converter) throws IOException, SAXException {
    List<RpcHeader> headers = new ArrayList<RpcHeader>();
    XPathWorkspace w = workspace.get();/*from w  ww .  j a  v a 2  s  .  co  m*/
    try {
        Node envelope = w.docBuilder.parse(is).getDocumentElement();
        Node header = findFirstChildNamed(envelope, "Header");
        if (header != null)
            for (Node h = header.getFirstChild(); h != null; h = h.getNextSibling()) {
                if (!(h instanceof Element))
                    continue;
                headers.add(new RpcHeader(h.getNamespaceURI(), h.getLocalName(), h.getTextContent()));
            }

        Node body = findFirstChildNamed(envelope, "Body");
        for (Node res = body.getFirstChild(); res != null; res = res.getNextSibling()) {
            if (!(res instanceof Element))
                continue;
            if (res.getLocalName().equals(methodName + "Response")) {
                for (Node ret = res.getFirstChild(); ret != null; ret = ret.getNextSibling()) {
                    if (!(ret instanceof Element))
                        continue;
                    if (ret.getLocalName().equals(methodName + "Return")
                            || ret.getLocalName().equals(methodName + "Result")) {
                        try {
                            T r = nodeToType(w, ret, returnType, converter);
                            return new Trio<Collection<RpcHeader>, RpcFault, T>(headers, null, r);
                        } catch (IllegalArgumentException e) {
                            throw new CascadingIOException(e);
                        } catch (InstantiationException e) {
                            throw new CascadingIOException(e);
                        } catch (IllegalAccessException e) {
                            throw new CascadingIOException(e);
                        } catch (InvocationTargetException e) {
                            throw new CascadingIOException(e);
                        } catch (ParseException e) {
                            throw new CascadingIOException(e);
                        }
                    }
                }
            }
        }
        if (returnType.equals(void.class)) {
            return new Trio<Collection<RpcHeader>, RpcFault, T>(headers, null, null);
        }
        throw new IOException("not a valid SOAP message.");
    } finally {
        workspace.remove();
    }
}

From source file:Main.java

static String stringElement(Element row, String name) {
    final NodeList childNodes = row.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node node = childNodes.item(i);
        if (name.equals(node.getLocalName())) {
            String result = node.getTextContent();
            // If content is not plain text then returns name of
            // first child tag
            if (result == null && node.hasChildNodes()) {
                result = node.getFirstChild().getLocalName();
            }/*from  www  . ja  va  2s.c  o  m*/
            return result;
        }
    }
    return null;
}

From source file:ua.kiev.doctorvera.utils.SMSGateway.java

public static final String getElementValue(Node elem) {
    Node child;/*from  ww w .j av a2  s .c  o m*/
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}