Example usage for org.w3c.dom Node getTextContent

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

Introduction

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

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

private static Object nodeValue(Node node) {
    NodeList childList = node.getChildNodes();
    if (childList.getLength() == 0) {
        return "";
    } else if (childList.getLength() == 1 && childList.item(0).getNodeType() == Node.TEXT_NODE) {
        return node.getTextContent().trim();
    } else {//from w  w w  .  j  a  v a 2 s  .  co m
        List<Object> value = new ArrayList<Object>();
        for (int i = 0; i < childList.getLength(); i++) {
            value.add(nodeValue(childList.item(i)));
        }
        return value;
    }
}

From source file:com.threewks.thundr.googleapis.cloudstorage.GoogleCloudStorageServiceImpl.java

static String findNamedValue(Node node, String name) {
    Node child = findNamedChild(node, name);
    return child == null ? null : StringUtils.trimToEmpty(child.getTextContent());
}

From source file:com.alta189.cyborg.commit.ShortUrlService.java

public static String shorten(String url, String keyword) {
    switch (service) {
    case BIT_LY:/*from   w  w w.  j  av a  2s.  co m*/
        HttpClient httpclient = new HttpClient();
        HttpMethod method = new GetMethod("http://api.bit.ly/shorten");
        method.setQueryString(
                new NameValuePair[] { new NameValuePair("longUrl", url), new NameValuePair("version", "2.0.1"),
                        new NameValuePair("login", user), new NameValuePair("apiKey", apiKey),
                        new NameValuePair("format", "xml"), new NameValuePair("history", "1") });
        try {
            httpclient.executeMethod(method);
            String responseXml = method.getResponseBodyAsString();
            String retVal = null;
            if (responseXml != null) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                StringReader st = new StringReader(responseXml);
                Document d = db.parse(new InputSource(st));
                NodeList nl = d.getElementsByTagName("shortUrl");
                if (nl != null) {
                    Node n = nl.item(0);
                    retVal = n.getTextContent();
                }
            }

            return retVal;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return null;
    case SPOUT_IN:
        HttpClient client = new HttpClient();
        String result = null;
        client.getParams().setParameter("http.useragent", "Test Client");

        BufferedReader br = null;
        PostMethod pMethod = new PostMethod("http://spout.in/yourls-api.php");
        pMethod.addParameter("signature", apiKey);
        pMethod.addParameter("action", "shorturl");
        pMethod.addParameter("format", "simple");
        pMethod.addParameter("url", url);
        if (keyword != null) {
            pMethod.addParameter("keyword", keyword);
        }

        try {
            int returnCode = client.executeMethod(pMethod);

            if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                System.err.println("The Post method is not implemented by this URI");
                pMethod.getResponseBodyAsString();
            } else {
                br = new BufferedReader(new InputStreamReader(pMethod.getResponseBodyAsStream()));
                String readLine;
                if (((readLine = br.readLine()) != null)) {
                    result = readLine;
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        } finally {
            pMethod.releaseConnection();
            if (br != null) {
                try {
                    br.close();
                } catch (Exception fe) {
                    fe.printStackTrace();
                }
            }
        }
        return result;
    }
    return null;
}

From source file:Main.java

/** Extract all of the child nodes as a Properties object from a node in an XML document */
public static Properties extractChildNodes(Document document) {
    try {/*from   w  ww  .  j  ava 2  s  . c o  m*/
        Properties props = new Properties();
        Element top = document.getDocumentElement();
        NodeList children = top.getChildNodes();
        Node child;
        String name;
        String value;
        for (int i = 0; i < children.getLength(); i++) {
            child = children.item(i);
            name = child.getNodeName();
            value = child.getTextContent().trim();
            props.setProperty(name, value);
        }
        return props;
    } catch (Exception e) {
        return new Properties();
    }
}

From source file:Main.java

public static String getXMLValueByName(String xmlFilePath, String name) {
    String returnValue = "No Value!";
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    try {//from w  w  w.java 2  s  . c  om
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = builder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(name)) {
                        returnValue = node.getTextContent().trim();
                    }
                }
            }
        }
    } catch (Exception e) {

    }
    return returnValue;
}

From source file:com.cloud.test.utils.UtilsForTest.java

public static Map<String, List<String>> getMultipleValuesFromXML(InputStream is, String[] tagNames) {
    Map<String, List<String>> returnValues = new HashMap<String, List<String>>();
    try {/* w w w .java  2 s.c o  m*/
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);
        Element rootElement = doc.getDocumentElement();
        for (int i = 0; i < tagNames.length; i++) {
            NodeList targetNodes = rootElement.getElementsByTagName(tagNames[i]);
            if (targetNodes.getLength() <= 0) {
                System.out.println("no " + tagNames[i] + " tag in XML response...returning null");
            } else {
                List<String> valueList = new ArrayList<String>();
                for (int j = 0; j < targetNodes.getLength(); j++) {
                    Node node = targetNodes.item(j);
                    valueList.add(node.getTextContent());
                }
                returnValues.put(tagNames[i], valueList);
            }
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return returnValues;
}

From source file:cz.incad.kramerius.utils.solr.SolrUtils.java

/**
 * Disects pid paths from given parsed solr document
 * @return pid paths/*from  w ww. j a va2s.co  m*/
 * @throws XPathExpressionException cannot disect pid paths
 */
public static List<String> disectPidPaths(Document parseDocument) throws XPathExpressionException {
    synchronized (parseDocument) {
        List<String> list = new ArrayList<String>();
        NodeList paths = (org.w3c.dom.NodeList) pidPathExpr().evaluate(parseDocument, XPathConstants.NODESET);
        if (paths != null) {
            for (int i = 0, ll = paths.getLength(); i < ll; i++) {
                Node n = paths.item(i);
                String text = n.getTextContent();
                list.add(text.trim());
            }
            return list;
        }
        return new ArrayList<String>();
    }
}

From source file:Main.java

/**
 * @return a list of all child node text contents with this name
 *///ww  w .  ja va  2  s .c o  m
public static ArrayList<String> getChildNodesTextContents(Node node, String name) {
    ArrayList<String> results = new ArrayList<String>();

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) {
            results.add(temp.getTextContent());
        }
    }

    return (results);
}

From source file:cz.incad.kramerius.utils.solr.SolrUtils.java

/**
 * Disect models path from given solr document
 * @param parseDocument Parsed solr document
 * @return model paths//from   w  w  w.  jav a2 s  . c  o m
 * @throws XPathExpressionException cannot disect models path
 */
public static List<String> disectModelPaths(Document parseDocument) throws XPathExpressionException {
    synchronized (parseDocument) {
        List<String> list = new ArrayList<String>();
        NodeList pathNodes = (NodeList) modelPathExpr().evaluate(parseDocument, XPathConstants.NODESET);
        if (pathNodes != null) {
            for (int i = 0, ll = pathNodes.getLength(); i < ll; i++) {
                Node n = pathNodes.item(i);
                String text = n.getTextContent();
                list.add(text.trim());
            }
            return list;
        }
        return new ArrayList<String>();
    }
}

From source file:com.hueemulator.lighting.utils.TestUtils.java

private static Set<Object> getChildSet(Node node, String basePath) {
    Set<Object> childSet = new HashSet<Object>();
    if (!node.hasChildNodes() && !node.getTextContent().trim().equals("")) {
        childSet.add(basePath + ":" + node.getTextContent());
    } else {/*w  w w. ja  v a 2s .  com*/
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            childSet.add(getChildSet(children.item(i), basePath + "/" + node.getNodeName()));
        }
    }
    return childSet;
}