Example usage for org.w3c.dom Element getTextContent

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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:org.kitodo.sruimport.ResponseHandler.java

private static String getRecordTitle(Element record) {
    Element modsElement = getXmlElement(record, MODS_TAG);
    Element recordTitle = getXmlElement(modsElement, RECORD_TITLE_TAG);
    return recordTitle.getTextContent().trim();
}

From source file:Main.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;/*w w  w  .jav a 2 s.  c o m*/
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            Element childElement = (Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static <T> List<T> array(final Element doc, final String attributeName, Class<T> clz) {
    synchronized (doc.getOwnerDocument()) {
        List<T> ret = new ArrayList<T>();
        List<Element> elms = XMLUtils.getElements(doc, new XMLUtils.ElementsFilter() {

            @Override// www  .j  av  a  2 s.  com
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals("arr") && element.hasAttribute("name")
                        && element.getAttribute("name").equals(attributeName));
            }
        });
        for (Element e : elms) {
            ret.add(value(e.getTextContent(), clz));
        }
        return ret;
    }
}

From source file:it.cilea.osd.common.utils.XMLUtils.java

/**
 * //from   w w w  .j  ava  2 s.co m
 * @param rootElement
 *            l'elemento in cui cercare
 * @param subElementName
 *            il nome del sottoelemento di cui estrarre il valore
 * @return una lista di stringhe contenente tutti i valori del sottoelemento
 *         cercato. Null se non sono presenti sottoelementi o l'elemento
 *         radice se null
 */
public static List<String> getElementValueList(Element rootElement, String subElementName) {
    if (rootElement == null)
        return null;

    List<Element> subElements = getElementList(rootElement, subElementName);
    if (subElements == null)
        return null;

    List<String> result = new LinkedList<String>();
    for (Element el : subElements) {
        if (StringUtils.isNotBlank(el.getTextContent())) {
            result.add(el.getTextContent().trim());
        }
    }
    return result;
}

From source file:Main.java

/**
 * Returns the boolean value of the text content for the child element with the specified name
 * for the specified element./*from   w  ww. ja v a  2  s  . c om*/
 *
 * @param element the parent element
 * @param name    the name of the child element to return
 *
 * @return the boolean value of the text content for the child element or <code>null</code> if a
 *         child element with the specified name could not be found
 */
public static Boolean getChildElementBoolean(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                try {
                    return Boolean.parseBoolean(childElement.getTextContent());
                } catch (Throwable e) {
                    throw new RuntimeException("Failed to parse the invalid boolean value ("
                            + childElement.getTextContent() + ")");
                }
            }
        }
    }

    return null;
}

From source file:Main.java

private final static String getElement(String filePath, String key) throws Exception {

    String value = null;//from  w ww.  j av a2  s.c om

    try {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        File xmlFile = new File(filePath);

        Document document = documentBuilder.parse(xmlFile);

        Element resourcesElement = document.getDocumentElement();
        resourcesElement.normalize();

        NodeList strings = resourcesElement.getChildNodes();

        int stringsCount = strings.getLength();

        for (int i = 0; i < stringsCount; i++) {

            Node node = strings.item(i);

            short nodeType = node.getNodeType();

            if (nodeType == Node.ELEMENT_NODE) {

                Element element = (Element) node;

                String attribute = element.getAttribute("name");

                if (attribute.equals(key)) {

                    value = element.getTextContent();
                }
            }
        }

    } catch (Exception ex) {

        throw ex;
    }

    return value;
}

From source file:com.atomiton.watermanagement.ngo.util.WaterMgmtNGOUtility.java

public static String getXMLElementValue(String tagName, String xmlString) {
    try {//from   ww w . j a  v a 2 s .c  o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xmlString)));
        Element rootElement = document.getDocumentElement();
        String rootElementTagName = rootElement.getTagName();
        if (rootElementTagName.equalsIgnoreCase(tagName)) {
            return rootElement.getTextContent();
        } else {
            NodeList list = rootElement.getElementsByTagName(tagName);
            if (list != null && list.getLength() > 0) {
                NodeList subList = list.item(0).getChildNodes();
                if (subList != null && subList.getLength() > 0) {
                    return subList.item(0).getNodeValue();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * Disect pid from given solr document// w  ww.j a  v a  2s.  c o  m
 * @param parseDocument Parsed solr document
 * @return PID 
 * @throws XPathExpressionException cannot disect pid
 */
public static String disectPid(Document parseDocument) throws XPathExpressionException {
    synchronized (parseDocument) {
        Node pidNode = (Node) docPidExpr().evaluate(parseDocument, XPathConstants.NODE);
        if (pidNode != null) {
            Element pidElm = (Element) pidNode;
            return pidElm.getTextContent().trim();
        }
        return null;
    }
}

From source file:org.kitodo.sruimport.ResponseHandler.java

private static int extractNumberOfRecords(Document document) {
    NodeList numberOfRecordNodes = document.getElementsByTagNameNS(SRW_NAMESPACE, SRW_NUMBER_OF_RECORDS_TAG);
    assert numberOfRecordNodes.getLength() == 1;
    Element numberOfRecordsElement = (Element) numberOfRecordNodes.item(0);
    if (Objects.nonNull(numberOfRecordsElement)) {
        return Integer.parseInt(numberOfRecordsElement.getTextContent().trim());
    }//from  w  w w  .  ja v a2s. c  om
    return 0;
}

From source file:Main.java

/**
 * Get the value of a node specified by a starting node and a
 * path string. If the starting node is a Document, use the
 * document element as the starting point.
 * <ul>//from  ww  w .j  av a2  s . c o m
 * <li>A path to an element has the form: elem1/.../elemN
 * <li>A path to an attribute has the form: elem1/.../elemN@attr
 * </ul>
 * The value of an element node is the sum of all the element's
 * first generation child text nodes. Note that this is not what you
 * would get from a mixed element in an XSL program.
 * @param node the top of the tree to search.
 * @param path the path from the top of the tree to the desired node.
 * @return the value of the first node matching the path, or the
 * empty string if no node exists at the path location or if the
 * starting node is not an element.
 */
public static String getValueViaPath(Node node, String path) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return "";
    path = path.trim();
    int kAtsign = path.indexOf("@");

    //If the target is an element, get the element's value.
    if (kAtsign == -1) {
        Element target = getElementViaPath(node, path);
        if (target == null)
            return "";
        return target.getTextContent();
    }

    //The target is an attribute; first find the element.
    String subpath = path.substring(0, kAtsign);
    Element target = getElementViaPath(node, subpath);
    if (target == null)
        return null;
    String name = path.substring(kAtsign + 1);
    return target.getAttribute(name);
}