Example usage for org.w3c.dom Element getElementsByTagName

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

Introduction

In this page you can find the example usage for org.w3c.dom Element getElementsByTagName.

Prototype

public NodeList getElementsByTagName(String name);

Source Link

Document

Returns a NodeList of all descendant Elements with a given tag name, in document order.

Usage

From source file:com.flipkart.phantom.runtime.impl.spring.utils.ConfigFileUtils.java

/**
 * Gets the task handler names from Config file
 * @param configFile job config file contents as a <code> ByteArrayResource </code>
 * @return List of task handler names, null if unable to find a TaskHandler name.
 *//* w  w  w .  j av  a2s . c  o m*/
public static List<String> getHandlerNames(ByteArrayResource configFile) {
    List<String> jobNameList = new LinkedList<String>();
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(configFile.getInputStream());
        Element docEle = dom.getDocumentElement();
        //get a nodelist of nodes with the name "ConfigFileUtils.BATCH_JOB_TAG" 
        NodeList nl = docEle.getElementsByTagName(ConfigFileUtils.BATCH_JOB_TAG);
        //Loop over all found nodes
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                //get the element
                Element el = (Element) nl.item(i);
                if (el.hasAttribute(ConfigFileUtils.ID_PROP)) {
                    jobNameList.add(el.getAttribute(ConfigFileUtils.ID_PROP));
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to get the job name from the given Spring Batch configuration file", e);
        throw new PlatformException(e);
    }
    return jobNameList;
}

From source file:Main.java

public static Element firstChildElement(Element element, String childNameSpace, String childName)
        throws NoSuchElementException {
    NodeList childCandidateList;/* w w  w. j av a  2 s. co  m*/
    if (childNameSpace == null || childNameSpace.isEmpty()) {
        childCandidateList = element.getElementsByTagName(childName);
    } else {
        /*childCandidateList = element.getElementsByTagNameNS(childNameSpace,
          childName);*/
        childCandidateList = element.getElementsByTagNameNS(childNameSpace, childName);
    }
    if (childCandidateList.getLength() > 0) {
        Element firstChild = (Element) childCandidateList.item(0);
        return firstChild;
    } else {
        throw new NoSuchElementException("No child candidate found in this element");
    }
}

From source file:eu.europa.esig.dss.pades.signature.visible.ImageFactory.java

private static void initDpi(IIOMetadata metadata, int dpi) throws IIOInvalidTreeException {
    Element tree = (Element) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
    Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
    jfif.setAttribute("Xdensity", Integer.toString(dpi));
    jfif.setAttribute("Ydensity", Integer.toString(dpi));
    jfif.setAttribute("resUnits", "1"); // density is dots per inch
    metadata.setFromTree("javax_imageio_jpeg_image_1.0", tree);
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static String[] getTexts(Element root, String elementName) {
    NodeList list = root.getElementsByTagName(elementName);
    String[] ret = new String[list.getLength()];
    for (int i = 0; i < list.getLength(); i++) {
        Text txt = (Text) list.item(i).getFirstChild();
        if (txt != null) {
            ret[i] = txt.getData();//from   w  w  w .j a va2s  . c  o m
        } else {
            ret[i] = ""; //$NON-NLS-1$
        }
    }
    return ret;
}

From source file:org.trpr.platform.batch.common.utils.ConfigFileUtils.java

/**
 * Gets the job names from Config file//w  ww  .j a v a  2 s. co m
 * @param configFile job config file contents as a <code> ByteArrayResource </code>
 * @return List of job names, null if unable to find a job name.
 */
public static List<String> getJobName(ByteArrayResource configFile) {
    List<String> jobNameList = new LinkedList<String>();
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(configFile.getInputStream());
        Element docEle = dom.getDocumentElement();
        //get a nodelist of nodes with the name "ConfigFileUtils.BATCH_JOB_TAG" 
        NodeList nl = docEle.getElementsByTagName(ConfigFileUtils.BATCH_JOB_TAG);
        //Loop over all found nodes
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                //get the element
                Element el = (Element) nl.item(i);
                if (el.hasAttribute(ConfigFileUtils.ID_PROP)) {
                    jobNameList.add(el.getAttribute(ConfigFileUtils.ID_PROP));
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to get the job name from the given Spring Batch configuration file", e);
        throw new PlatformException(e);
    }
    return jobNameList;
}

From source file:Main.java

/**
 * A convenience method that allows the extraction of text data from a named
 * child element of the given parent element.<br>
 * <br>//from   w w w. j a  v a  2 s.  com
 * <pre>
 * <parent-element>
 *     <child-element>[Text Data]</child-element>
 * </parent-element>
 * </pre>
 * 
 * @param parent The parent DOM element.
 * @param childTagName The name of the child element.
 * @return The text contained by the named child element.
 * @throws Exception If an error occurs while traversing the DOM objects.
 */
public static String getWrappedTextData(Element parent, String childTagName) throws Exception {
    NodeList nl = parent.getElementsByTagName(childTagName);

    if (nl.getLength() < 1) {
        return null;
    }

    Element childElement = (Element) nl.item(0);

    return getElementTextData(childElement, true);
}

From source file:Main.java

/**
 * Get one element by tag name and attribute value.
 * @return An Element object or null if no matching element.
 *//*from w w w . ja  v  a  2s.c o m*/
////
// METHOD: getOneElement
////
public static Element getOneElement(Element inParentElement, String inTagName, String inAttrName,
        String inAttrValue) {
    Element retElement = null;
    Element wkElement = null;
    try {
        NodeList wkNodeList = inParentElement.getElementsByTagName(inTagName);
        for (int ix = 0; ix < wkNodeList.getLength(); ++ix) {
            wkElement = (Element) wkNodeList.item(ix);
            if (inAttrValue.equals(wkElement.getAttribute(inAttrName))) {
                retElement = wkElement;
                break;
            }
        }
    } catch (Exception excp) {
        ;
    }

    return retElement;
}

From source file:resources.XmlToAnime.java

public static void ReadXml() {
    animes.clear();/*  w ww.j  ava  2 s.c  o  m*/
    if (sUrl.isEmpty() || sUrl == null) {
        throw new InternalError("Url should not be empty!");
    }

    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        //Document doc = (Document) dBuilder.parse(new URL(sUrl).openStream());
        //Document doc = (Document) dBuilder.parse

        InputStream input = getInput();
        if (input.available() > 0) {
            Document doc = (Document) dBuilder.parse(input);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("entry");

            for (int i = 0; i < nList.getLength(); i++) {
                Node nNode = nList.item(i);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Anime anime = new Anime();
                    Element eElement = (Element) nNode;
                    anime.setId(Integer.parseInt(eElement.getElementsByTagName("id").item(0).getTextContent()));
                    anime.setTitle(eElement.getElementsByTagName("title").item(0).getTextContent());
                    anime.setDescription(eElement.getElementsByTagName("synopsis").item(0).getTextContent());
                    anime.setEpisodes(Integer
                            .parseInt(eElement.getElementsByTagName("episodes").item(0).getTextContent()));
                    anime.setImageUrl(eElement.getElementsByTagName("image").item(0).getTextContent());
                    anime.setStartDate(eElement.getElementsByTagName("start_date").item(0).getTextContent());
                    anime.setEndDate(eElement.getElementsByTagName("end_date").item(0).getTextContent());

                    String temp = eElement.getElementsByTagName("status").item(0).getTextContent();
                    if (temp.toLowerCase().contains("finished")) {
                        anime.setStatus(AnimeStatus.FINISHED);
                    } else if (temp.toLowerCase().contains("currently")) {
                        anime.setStatus(AnimeStatus.CURRENTLY_AIRING);
                    }

                    anime.setAirDay(CalculateNextRelease.getAirDay(anime));
                    anime.setType(
                            Anime.TYPE.valueOf(eElement.getElementsByTagName("type").item(0).getTextContent()));
                    animes.add(anime);
                }
            }
        }

    } catch (SAXException | IOException | ParserConfigurationException e) {
        status = STATUS.ERROR;
        e.printStackTrace();
    }
}

From source file:Main.java

public static NodeList findNodes(Element parentNode, String nodeName) {
    if (parentNode == null)
        throw new NullPointerException("Parent Node cannot be null!");
    if (nodeName == null)
        throw new NullPointerException("Nodename cannot be null!");
    return parentNode.getElementsByTagName(nodeName);
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

/**
 * Renvoie la valeur de l'attribut donn, d'un lment donn qui doit tre
 * unique sous l'lment racine//from   w w w  . ja  va 2s .  co m
 * 
 * @param root
 * @param elementName
 * @param attribute
 * @return
 */
public static String getElementAttribute(Element root, String elementName, String attribute) {
    NodeList list = root.getElementsByTagName(elementName);
    if (list.getLength() == 0) {
        return null;
    }
    return ((Element) list.item(0)).getAttribute(attribute);
}