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:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java

public static void replacePidsInDOM(Element docE) {
    String[] apiReplace = KConfiguration.getInstance().getAPIPIDReplace();
    for (String k : apiReplace) {
        if (k.equals("PID"))
            continue; // already replaced
        Element foundElm = findSolrElement(docE, k);
        if (foundElm != null) {

            if (foundElm.getNodeName().equals("str")) {
                String value = SOLRUtils.value(foundElm.getTextContent(), String.class);
                if (value != null && (value.indexOf("/@") > 0)) {
                    value = value.replace("/@", "@");
                    foundElm.setTextContent(value);
                }//from   w  ww.j a v  a 2  s  .c om
            } else if (foundElm.getNodeName().equals("arr")) {
                List<String> array = SOLRUtils.array(docE, k, String.class);
                List<String> newArray = new ArrayList<String>();
                for (String value : array) {
                    value = value.replace("/@", "@");
                    newArray.add(value);
                }

                docE.removeChild(foundElm);
                Element newArrElm = SOLRUtils.arr(foundElm.getOwnerDocument(), k, newArray);
                docE.appendChild(newArrElm);
            } else {
                LOGGER.warning("skipping object type '" + foundElm.getNodeName() + "'");
            }

        }
    }

}

From source file:com.bluexml.side.form.utils.DOMUtil.java

/**
 * Retrieves a specific element based on tag name anywhere below a node with the additional
 * condition that its text content is not empty.
 * // w  ww .jav  a 2 s.  c  o m
 * @param rootElt
 *            the root of the tree to search
 * @param tagName
 * @return the first Element that matches the tagName
 */
public static Element getElementInDescentByNameNonNull(Element rootElt, String tagName) {
    Element res = null;
    if (rootElt != null) {
        List<Element> children = getAllChildren(rootElt);
        res = getOneElementByTagName(children, tagName);
        if (res != null) {
            if (StringUtils.trimToNull(res.getTextContent()) != null) {
                return res;
            }
            return null;
        }
        for (Element elt : children) {
            res = getElementInDescentByNameNonNull(elt, tagName);
            if (res != null) {
                return res;
            }
        }
    }
    return null;
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

private static void readPoolProperties(Document xmlConfiguration) {
    poolProperties = new HashMap<String, String>();

    Element doc = xmlConfiguration.getDocumentElement();
    NodeList jdbcPoolNodes = doc.getElementsByTagName("jdbc-pool");
    Node jdbcPoolNode = jdbcPoolNodes.item(0);

    if (jdbcPoolNode != null) {
        NodeList poolPropertiesNodes = jdbcPoolNode.getChildNodes();

        // iterate over the properties
        for (int i = 0; i < poolPropertiesNodes.getLength(); i++) {
            // get the pool property node
            Node poolPropertiesNode = poolPropertiesNodes.item(i);

            if (poolPropertiesNode instanceof Element) {
                Element poolPropertiesElement = (Element) poolPropertiesNode;

                String name = poolPropertiesElement.getLocalName();
                String value = poolPropertiesElement.getTextContent();

                if (!StringUtils.isEmpty(value)) {
                    poolProperties.put(name, value);
                }//from   w  ww.j a v a2s  . com
            }
        }
    }
}

From source file:org.yamj.core.service.metadata.nfo.InfoReader.java

/**
 * Parse Trailers from the XML NFO file/*from   w  w  w  .  j  a v a 2 s.  c o m*/
 *
 * @param nlElements
 * @param dto
 */
private static void parseTrailers(NodeList nlElements, InfoDTO dto) {
    Node nElements;
    for (int looper = 0; looper < nlElements.getLength(); looper++) {
        nElements = nlElements.item(looper);
        if (nElements.getNodeType() == Node.ELEMENT_NODE) {
            Element eTrailer = (Element) nElements;
            dto.addTrailerURL(eTrailer.getTextContent());
        }
    }
}

From source file:org.yamj.core.service.metadata.nfo.InfoReader.java

/**
 * Parse directors from the XML NFO file
 *
 * @param nlElements/*www  . j  av a 2  s .c o  m*/
 * @param dto
 */
private static void parseDirectors(NodeList nlElements, InfoDTO dto) {
    // check if we have a node
    if (nlElements == null || nlElements.getLength() == 0) {
        return;
    }

    Node nElements;
    for (int looper = 0; looper < nlElements.getLength(); looper++) {
        nElements = nlElements.item(looper);
        if (nElements.getNodeType() == Node.ELEMENT_NODE) {
            Element eDirector = (Element) nElements;
            dto.addDirector(eDirector.getTextContent());
        }
    }
}

From source file:org.yamj.core.service.metadata.nfo.InfoReader.java

/**
 * Parse all the IDs associated with the movie from the XML NFO file
 *
 * @param nlElements// w  ww .jav a 2 s  .com
 * @param dto
 * @param isTV
 */
private static void parseIds(NodeList nlElements, InfoDTO dto, boolean isTV) {
    Node nElements;
    for (int looper = 0; looper < nlElements.getLength(); looper++) {
        nElements = nlElements.item(looper);
        if (nElements.getNodeType() == Node.ELEMENT_NODE) {
            Element eId = (Element) nElements;

            String movieId = eId.getTextContent();
            if (StringUtils.isNotBlank(movieId)) {
                String movieDb = eId.getAttribute("moviedb");
                if (StringUtils.isBlank(movieDb)) {
                    if ("-1".equals(movieId)) {
                        // skip all scans
                        dto.setSkipAllOnlineScans();
                    } else {
                        // choose default scanner id
                        if (isTV) {
                            movieDb = TheTVDbScanner.SCANNER_ID;
                        } else {
                            movieDb = ImdbScanner.SCANNER_ID;
                        }
                        dto.addId(movieDb, movieId);
                        LOG.debug("Found {} ID: {}", movieDb, movieId);
                    }
                } else {
                    dto.addId(movieDb, movieId);
                    LOG.debug("Found {} ID: {}", movieDb, movieId);
                }
            }

            // process the TMDB id
            movieId = eId.getAttribute("TMDB");
            if (StringUtils.isNotBlank(movieId)) {
                LOG.debug("Found TheMovieDb ID: {}", movieId);
                dto.addId(TheMovieDbScanner.SCANNER_ID, movieId);
            }
        }
    }
}

From source file:org.shareok.data.documentProcessor.DocumentProcessorUtil.java

/**
 *
 * @param filePath : file path/*from  w w  w .  j  av a2 s. c om*/
 * @param tagName : tag name
 * @param attributeMap : the map of attribute name-value map. Note: no null value contained in this map
 * @return : value of the elements
 */
public static String[] getDataFromXmlByTagNameAndAttributes(String filePath, String tagName,
        Map<String, String> attributeMap) {
    String[] data = null;
    try {
        File file = new File(filePath);

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName(tagName);

        int length = nList.getLength();

        if (length == 0) {
            return null;
        }

        List<String> dataList = new ArrayList<>();
        for (int temp = 0; temp < length; temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                boolean attrMatched = true;
                Element eElement = (Element) nNode;
                for (String att : attributeMap.keySet()) {
                    String val = attributeMap.get(att);
                    if (!eElement.hasAttribute(att) || !val.equals(eElement.getAttribute(att))) {
                        attrMatched = false;
                    }
                }
                if (attrMatched == true) {
                    dataList.add(eElement.getTextContent());
                }
            }
        }

        int size = dataList.size();
        data = new String[size];
        data = dataList.toArray(data);

    } catch (ParserConfigurationException | SAXException | IOException | DOMException ex) {
        logger.error("Cannot get the data from file at " + filePath + " by tag name " + tagName
                + " and attributes map", ex);
    }
    return data;
}

From source file:com.evolveum.midpoint.prism.parser.DomParser.java

private static <T> T parsePrimitiveElementValue(Element element, QName typeName) throws SchemaException {
    if (ItemPath.XSD_TYPE.equals(typeName)) {
        return (T) parsePath(element);
    } else if (DOMUtil.XSD_QNAME.equals(typeName)) {
        return (T) DOMUtil.getQNameValue(element);
    } else if (XmlTypeConverter.canConvert(typeName)) {
        return (T) XmlTypeConverter.toJavaValue(element, typeName);
    } else if (DOMUtil.XSD_ANYTYPE.equals(typeName)) {
        return (T) element.getTextContent(); // if parsing primitive as xsd:anyType, we can safely parse it as string
    } else {//from  w w  w .  j ava  2  s.c o m
        throw new SchemaException("Cannot convert element '" + element + "' to " + typeName);
    }
}

From source file:org.yamj.core.service.metadata.nfo.InfoReader.java

/**
 * Parse Sets from the XML NFO file/*  ww  w  . j  a  v  a  2 s  .co  m*/
 *
 * @param nlElements
 * @param dto
 */
private static void parseSets(NodeList nlElements, InfoDTO dto) {
    Node nElements;
    for (int looper = 0; looper < nlElements.getLength(); looper++) {
        nElements = nlElements.item(looper);
        if (nElements.getNodeType() == Node.ELEMENT_NODE) {
            Element eId = (Element) nElements;

            String setOrder = eId.getAttribute("order");
            if (StringUtils.isNumeric(setOrder)) {
                dto.addSetInfo(eId.getTextContent(), Integer.parseInt(setOrder));
            } else {
                dto.addSetInfo(eId.getTextContent());
            }
        }
    }
}

From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java

public static <T> T toJavaValue(Element xmlElement, Class<T> type) throws SchemaException {
    if (type.equals(Element.class)) {
        return (T) xmlElement;
    } else if (type.equals(QName.class)) {
        return (T) DOMUtil.getQNameValue(xmlElement);
    } else if (PolyString.class.isAssignableFrom(type)) {
        return (T) polyStringToJava(xmlElement);
    } else {/*  w w  w  .j av a2  s  . c o  m*/
        String stringContent = xmlElement.getTextContent();
        if (stringContent == null) {
            return null;
        }
        T javaValue = toJavaValue(stringContent, type);
        if (javaValue == null) {
            throw new IllegalArgumentException(
                    "Unknown type for conversion: " + type + "(element " + DOMUtil.getQName(xmlElement) + ")");
        }
        return javaValue;
    }
}