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 HashMap<String, String> getElementsByTagNameHashMap(String sTag, Element eElement) {
    String value = null;//from www.java 2  s  .c o  m
    HashMap<String, String> map = new HashMap<String, String>();
    NodeList nlList = eElement.getElementsByTagName(sTag);
    if (nlList != null && nlList.item(0) != null) {
        NodeList nodes = nlList.item(0).getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            map.put(node.getNodeName(), node.getTextContent());
        }
    }
    return map;
}

From source file:Main.java

static public List<String> getNodeTexts(Node node, String... nodePath) {
    List<Node> nodes = getNodes(node, nodePath);
    if (nodes != null) {
        List<String> strs = new ArrayList<String>(nodes.size());
        for (Node n : nodes) {
            strs.add(n.getTextContent());
        }/*ww  w  .  jav a  2s .  c om*/
        return strs;
    } else {
        return null;
    }
}

From source file:Main.java

public static void removeHandset(String file, String name) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);
    NodeList devices = doc.getElementsByTagName("devices");
    NodeList nodeList = doc.getElementsByTagName("device");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node deviceNode = nodeList.item(i);
        if (deviceNode.getTextContent().equals(name)) {
            devices.item(0).removeChild(deviceNode);
        }//from   w  ww .  j a  v  a  2 s. c o  m
    }

    //save
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    Properties props = t.getOutputProperties();
    props.setProperty(OutputKeys.ENCODING, "GB2312");
    t.setOutputProperties(props);
    DOMSource dom = new DOMSource(doc);
    StreamResult sr = new StreamResult(file);
    t.transform(dom, sr);
}

From source file:Main.java

/**
 * @param the/* w w w  . j a  v a2  s .c om*/
 *            node to create the name from, never <code>null</code>
 * @return the node of the name as qualified name, never <code>null</code>
 */
public static QName buildQName(Node node) {
    String localPart;
    String nsName;
    String name = node.getTextContent();
    int indexOfColon = name.indexOf(':');
    if (indexOfColon > 0) {
        localPart = name.substring(indexOfColon + 1);
        nsName = node.lookupNamespaceURI(name.substring(0, indexOfColon));
    } else {
        localPart = name;
        // return default namespace URI if any
        nsName = node.lookupNamespaceURI(null);
    }
    return new QName(nsName, localPart);
}

From source file:Main.java

public static String getText(Node elem) {
    if (elem.getNodeType() == Element.TEXT_NODE)
        return elem.getTextContent();
    else if (elem.hasChildNodes())
        return elem.getFirstChild().getTextContent();
    return "";
}

From source file:Main.java

/**
 * Parses the value of the given attribute as an boolean.
 * @param attributeName the name of the attribute.
 * @param map the map that contains all the attributes.
 * @return the float value./*from   w w w. j ava  2  s  . c  om*/
 */
public static boolean parseBoolean(String attributeName, NamedNodeMap map) {
    org.w3c.dom.Node attr = map.getNamedItem(attributeName);
    return attr != null ? Boolean.parseBoolean(attr.getTextContent()) : false;
}

From source file:Main.java

public static float getDescendentTextAsFloat(Node node, String name, float defaultValue) throws IOException {
    Node d = getDescendent(node, name);
    if (d != null) {
        String sval = d.getTextContent().trim();
        return Float.parseFloat(sval);
    }/*  w  w w. j a  v  a  2  s. c  o m*/
    return defaultValue;
}

From source file:Main.java

public static int getDescendentTextAsInt(Node node, String name, int defaultValue) throws IOException {
    Node d = getDescendent(node, name);
    if (d != null) {
        String sval = d.getTextContent().trim();
        return Integer.parseInt(sval);
    }/*w ww  .j  ava  2  s . co  m*/
    return defaultValue;
}

From source file:Main.java

public static String getChildNodeTextContent(Node parentNode, String childElementName) {
    Node node = getChildNode(parentNode, childElementName);
    if (node != null)
        return node.getTextContent();
    return null;//  ww  w.  j  a  va  2  s.co  m
}

From source file:Main.java

public static String xpathToContent(String xpathQuery, Object domObject) throws XPathExpressionException {
    Node node = xpathToNode(xpathQuery, domObject);

    if (node != null)
        return node.getTextContent();

    return null;//  w w  w. j a  v  a 2s.co  m
}