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 void resolveLeafNodeValue(Node node) {
    if (node != null) {
        Element element = (Element) node;
        NodeList childNodeList = element.getChildNodes();
        for (int j = 0; j < childNodeList.getLength(); j++) {
            Node chileNode = childNodeList.item(j);
            if (!chileNode.hasChildNodes()) {
                String nodeValue = resolveSystemProperty(chileNode.getTextContent());
                childNodeList.item(j).setTextContent(nodeValue);
            } else {
                resolveLeafNodeValue(chileNode);
            }//from   ww  w.  ja  v  a 2 s .  co  m
        }
    }
}

From source file:Main.java

public static Vector<HashMap> xmlToVector222(InputStream is, String xpath) {
    try {//from   w  w  w .  j a v  a2 s  . c  om
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        InputSource inputSource = new InputSource(is);

        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        Vector<HashMap> vector = new Vector<HashMap>();

        for (int x = 0; x < nodes.getLength(); x++) {
            NodeList nodeList = nodes.item(x).getChildNodes();
            HashMap hashmap = new HashMap();
            for (int y = 0; y < nodeList.getLength(); y++) {
                Node node = nodeList.item(y);
                if (!node.getNodeName().equals("#text")) {
                    hashmap.put(node.getNodeName(), node.getTextContent());
                }
            }
            vector.add(hashmap);
        }
        return vector;
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector();
    }
}

From source file:Main.java

public static String toString(NodeList nodes) {
    try {/*w w w .  j av a 2  s.co  m*/
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node item = nodes.item(i);
            switch (item.getNodeType()) {
            case Node.TEXT_NODE:
                stw.append(item.getTextContent());
                break;
            default:
                serializer.transform(new DOMSource(item), new StreamResult(stw));
            }
        }
        return stw.toString();
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

public static String getNodeValue(Node node) {
    String value = null;//from   ww  w  .  j a  va2  s  .  c o m
    if (node != null) {
        value = node.getNodeValue();
        if (value == null) {
            value = node.getTextContent();
        }
    }
    return value;
}

From source file:Main.java

public static String getPkey(final Node markitBondNode) {
    final NodeList markitBondNodeChildrenList = markitBondNode.getChildNodes();
    final int length = markitBondNodeChildrenList.getLength();
    for (int j = 0; j < length; ++j) {
        final Node childNode = markitBondNodeChildrenList.item(j);
        final String childName = childNode.getNodeName();
        if ("pkey".equals(childName)) {
            final String issuerIdString = childNode.getTextContent();
            return issuerIdString;
        }//  w  ww.  j a  v a2s .c o  m
    }
    return null;
}

From source file:Main.java

static void dumpXpath(Node node, PrintStream printer) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node item = list.item(i);
        printer.println("Xpath: " + getXPath(item));
        printer.println("Text Content: " + item.getTextContent());
        if (item.hasChildNodes()) {
            dumpXpath(item, printer);//from w  w  w .  j  a v a 2 s .co m
        }
    }
}

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }//  w  w  w. jav  a 2s  .  co m
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}

From source file:Main.java

private static String getNodeText(Element element, String nodeWeiZhi) {
    String result = "";
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                result += "," + node.getTextContent().trim();
            }//w  w  w.ja  va2  s .c om
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeText((Element) node, nodeWeiZhiTemp);
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Compares two DOM nodes for (deep) equality.
 * @param n1/* w w  w  . j a va 2s . com*/
 * @param n2
 * @return whether the nodes are equal
 */
public static final boolean compare(Node n1, Node n2) {
    boolean ret = true;
    ret &= n1.getLocalName().equals(n2.getLocalName());
    ret &= n1.getNamespaceURI().equals(n2.getNamespaceURI());
    String text = n1.getTextContent();
    if (text != null) {
        ret &= text.equals(n2.getTextContent());
    }
    NodeList children = n1.getChildNodes();
    ret &= (children.getLength() == n2.getChildNodes().getLength());
    for (int i = 0; ret && i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child instanceof Element) {
            ret &= compare(child, n2.getChildNodes().item(i));
        }
    }
    return ret;
}

From source file:Main.java

public static List<Map<String, String>> ReadFolderItemsFromFile(String path, String filename, String folderID) {

    List<Map<String, String>> results = new ArrayList<Map<String, String>>();

    try {/*from   ww  w. ja  v a2s  . co  m*/
        File fXmlFile = new File(path, filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = null;

        if (folderID == null) {
            // get all the children of the root
            nList = doc.getChildNodes().item(0).getChildNodes();
        } else {
            //Log.d("ReadItemsFromFile", "Current FolderID: " + folderID);

            NodeList folderList = doc.getElementsByTagName("folder");

            for (int i = 0; i < folderList.getLength(); i++) {

                Node curNode = folderList.item(i);
                if (curNode.getNodeType() == Node.ELEMENT_NODE) {
                    String curNodeId = ((Element) curNode).getElementsByTagName("id").item(0).getTextContent();
                    //Log.d("ReadItemsFromFile", "Number of items: " + curNodeId);

                    if (curNodeId.equals(folderID)) {
                        //Log.d("ReadItemsFromFile", "Found the folder");
                        NodeList folderChildren = curNode.getChildNodes();
                        for (int j = 0; j < folderChildren.getLength(); j++) {
                            //Log.d("ReadItemsFromFile", "node name: " + folderChildren.item(j).getNodeName());

                            if (folderChildren.item(j).getNodeName().equals("contents")) {
                                //Log.d("ReadItemsFromFile", "found the contents child");
                                nList = folderChildren.item(j).getChildNodes();
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }

        if (nList != null) {
            Log.d("ReadItemsFromFile", "-----------------------");
            Log.d("ReadItemsFromFile", "Number of items: " + nList.getLength());

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                //Log.d("ReadItemsFromFile", temp + ". node type: " + nNode.getNodeType());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    Node elementNameNode = eElement.getElementsByTagName("name").item(0);
                    String elementNameString = elementNameNode.getTextContent();
                    //Log.d("ReadItemsFromFile", "Name: " + elementNameString);
                    HashMap<String, String> mapElement = new HashMap<String, String>();
                    mapElement.put("name", elementNameString);
                    mapElement.put("id", eElement.getElementsByTagName("id").item(0).getTextContent());
                    mapElement.put("type", nNode.getNodeName());
                    results.add(mapElement);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return results;

}