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

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile(
            "/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");
    NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET);
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        System.out.println(node.getTextContent());
    }/*from  ww w . ja  va 2 s  . c  om*/
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fileInputStream = new FileInputStream(new File("src/file.xml"));
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc1 = builder.parse(fileInputStream);
    doc1.getDocumentElement().normalize();
    NodeList kList1 = doc1.getElementsByTagName("item");

    StringBuilder stringBuilder = new StringBuilder();

    for (int temp = 0; temp < kList1.getLength(); temp++) {
        Node kNode1 = kList1.item(temp);
        System.out.println("\nCurrent Element :" + kNode1.getNodeName());
        if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) kNode1;
            System.out.println("node name" + eElement.getNodeName());
            Node in = eElement.getFirstChild();
            if ((in.getTextContent() != null) && !(in.getTextContent()).isEmpty()
                    && !(in.getTextContent().length() == 0))
                stringBuilder.append(in.getTextContent());
        }//from w w w .ja  v  a 2  s . c  om
    }
    System.out.println(stringBuilder);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xPath = XPathFactory.newInstance().newXPath();
    FileInputStream file = new FileInputStream(new File("c:/data.xml"));

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = builderFactory.newDocumentBuilder();

    Document xmlDocument = builder.parse(file);

    XPathExpression expr = xPath.compile("//project/*");
    NodeList list = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        System.out.println(node.getNodeName() + "=" + node.getTextContent());
    }//  w w  w . j a v  a2 s. c o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder()
            .parse("http://stackoverflow.com/feeds/tag?tagnames=java&sort=newest");
    Element root = doc.getDocumentElement();
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expression = xPath.compile("//entry");
    NodeList nl = (NodeList) expression.evaluate(root, XPathConstants.NODESET);
    System.out.println("Found " + nl.getLength() + " items...");
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        expression = xPath.compile("title");
        Node child = (Node) expression.evaluate(node, XPathConstants.NODE);
        System.out.println(child.getTextContent());
    }/*  w w w.jav  a2  s . co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("Table.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath path = xFactory.newXPath();
    XPathExpression exp = path.compile("/tables/table");
    NodeList nlTables = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
    for (int tblIndex = 0; tblIndex < nlTables.getLength(); tblIndex++) {
        Node table = nlTables.item(tblIndex);
        Node nAtt = table.getAttributes().getNamedItem("title");
        System.out.println(nAtt.getTextContent());
        exp = path.compile("headings/heading");
        NodeList nlHeaders = (NodeList) exp.evaluate(table, XPathConstants.NODESET);
        Set<String> headers = new HashSet<String>(25);
        for (int index = 0; index < nlHeaders.getLength(); index++) {
            headers.add(nlHeaders.item(index).getTextContent().trim());
        }//from   ww w.  j  a va 2 s .com
        for (String header : headers) {
            System.out.println(header);
        }
        exp = path.compile("tablebody/tablerow");
        NodeList nlRows = (NodeList) exp.evaluate(table, XPathConstants.NODESET);
        for (int index = 0; index < nlRows.getLength(); index++) {
            Node rowNode = nlRows.item(index);
            exp = path.compile("tablecell/item");
            NodeList nlValues = (NodeList) exp.evaluate(rowNode, XPathConstants.NODESET);
            List<String> values = new ArrayList<String>(25);
            for (int valueIndex = 0; valueIndex < nlValues.getLength(); valueIndex++) {
                values.add(nlValues.item(valueIndex).getTextContent().trim());
            }
            for (String value : values) {
                System.out.println(value);
            }
        }
    }
}

From source file:com.twitter.hraven.hadoopJobMonitor.rpc.RestClient.java

/**
 * Used for testing the RestClient/*  ww  w . j  a  v a  2  s . c  om*/
 * 
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    Document xmlDoc = RestClient.getInstance()
            .getXml("http://localhost:8080/" + "ws/v1/history/mapreduce/jobs/job_1389724922546_0058/");
    // Iterating through the nodes and extracting the data.
    NodeList nodeList = xmlDoc.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        // We have encountered an <employee> tag.
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            System.out.println(node.getNodeName() + " = " + node.getTextContent());
        }
    }
}

From source file:Main.java

public static String getContent(Node node) {
    return node.getTextContent();
}

From source file:Main.java

public static String getNodeValue(Node node) {
    return node.getTextContent();
}

From source file:Main.java

public static String getNodeText(Node n) {
    return n.getTextContent().trim();
}

From source file:Main.java

private static String getTextContent(Node n) {
    return n == null ? "" : n.getTextContent();
}