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.mingo.parser.xml.dom.DomUtil.java

/**
 * Gets first tag occurrence.//from   w w  w.j ava 2  s  .c o m
 *
 * @param element element interface represents an element in an HTML or XML document
 * @param tagName tag name
 * @return found node
 */
public static Node getFirstTagOccurrence(Element element, String tagName) {
    Node firstNode = null;
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList != null && nodeList.getLength() > 0) {
        firstNode = nodeList.item(FIRST_ELEMENT);
    }
    return firstNode;
}

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

public static String getElementAttribute(Element dataRoot, String name, String attr) {
    NodeList nodeList = dataRoot.getElementsByTagName(name);
    Element element = null;//w w w .  j ava 2 s .c o  m
    if (nodeList != null && nodeList.getLength() > 0) {
        element = (Element) nodeList.item(0);
    }

    String attrValue = null;
    if (element != null) {
        attrValue = element.getAttribute(attr);
        if (StringUtils.isNotBlank(attrValue)) {
            attrValue = attrValue.trim();
        } else
            attrValue = null;
    }
    return attrValue;
}

From source file:com.microsoft.tfs.core.clients.build.internal.utils.XamlHelper.java

public static Properties loadPartial(final String xaml) {
    final Properties properties = new Properties();

    try {/*from w w  w .  j  a  v a 2 s. c  o  m*/
        final Document document = DOMCreateUtils.parseString(xaml);
        final Element root = document.getDocumentElement();

        final NodeList nodes = root.getElementsByTagName("x:String"); //$NON-NLS-1$
        for (int i = 0; i < nodes.getLength(); i++) {
            final Element element = (Element) nodes.item(i);
            final String key = element.getAttribute("x:Key"); //$NON-NLS-1$
            final String value = element.getFirstChild().getNodeValue();
            properties.put(key, value);
        }
    } catch (final XMLException e) {
        log.error(
                MessageFormat.format(Messages.getString("XamlHelper.ExceptionParsingProcessParaemtersFormat"), //$NON-NLS-1$
                        xaml), e);
    }

    return properties;
}

From source file:edu.indiana.d2i.datacatalog.dashboard.api.USStates.java

public static String getStates(String statesFilePath)
        throws ParserConfigurationException, IOException, SAXException {
    JSONObject statesFeatureCollection = new JSONObject();
    statesFeatureCollection.put("type", "FeatureCollection");
    JSONArray features = new JSONArray();

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//from  w w w  . j av  a  2 s.  co m
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document dom = documentBuilder.parse(new FileInputStream(new File(statesFilePath)));
        Element docElement = dom.getDocumentElement();
        NodeList states = docElement.getElementsByTagName("state");
        for (int i = 0; i < states.getLength(); i++) {
            Node state = states.item(i);
            JSONObject stateObj = new JSONObject();
            stateObj.put("type", "Feature");
            JSONObject geometry = new JSONObject();
            geometry.put("type", "Polygon");
            JSONArray coordinates = new JSONArray();
            JSONArray coordinateSub = new JSONArray();
            NodeList points = ((Element) state).getElementsByTagName("point");
            for (int j = 0; j < points.getLength(); j++) {
                Node point = points.item(j);
                JSONArray pointObj = new JSONArray();
                float lat = Float.parseFloat(((Element) point).getAttribute("lat"));
                float lng = Float.parseFloat(((Element) point).getAttribute("lng"));
                double trLng = lng * 20037508.34 / 180;
                double trLat = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
                pointObj.add(lng);
                pointObj.add(lat);
                coordinateSub.add(pointObj);
            }
            geometry.put("coordinates", coordinates);
            coordinates.add(coordinateSub);
            stateObj.put("geometry", geometry);
            JSONObject name = new JSONObject();
            name.put("Name", ((Element) state).getAttribute("name"));
            name.put("colour", "#FFF901");
            stateObj.put("properties", name);
            features.add(stateObj);
        }
        statesFeatureCollection.put("features", features);
        return statesFeatureCollection.toJSONString();
    } catch (ParserConfigurationException e) {
        log.error("Error while processing states.xml.", e);
        throw e;
    } catch (FileNotFoundException e) {
        log.error("Error while processing states.xml.", e);
        throw e;
    } catch (SAXException e) {
        log.error("Error while processing states.xml.", e);
        throw e;
    } catch (IOException e) {
        log.error("Error while processing states.xml.", e);
        throw e;
    }
}

From source file:com.omertron.yamjtrakttv.tools.CompleteMoviesTools.java

/**
 * Get the season and episode information from the files section for TV
 * Shows//w  w w.j a  v a 2  s .c  o  m
 *
 * @param eVideo
 * @return
 */
private static List<Episode> parseTvFiles(Element eVideo) {
    List<Episode> episodes = new ArrayList<>();

    NodeList nlFile = eVideo.getElementsByTagName("file");
    if (nlFile.getLength() > 0) {
        Node nFile;
        Element eFile;
        for (int loop = 0; loop < nlFile.getLength(); loop++) {
            nFile = nlFile.item(loop);
            if (nFile.getNodeType() == Node.ELEMENT_NODE) {
                eFile = (Element) nFile;
                int season = Integer.parseInt(DOMHelper.getValueFromElement(eFile, "season"));
                int firstPart = Integer.parseInt(eFile.getAttribute("firstPart"));
                int lastPart = Integer.parseInt(eFile.getAttribute("lastPart"));
                boolean watched = Boolean.parseBoolean(DOMHelper.getValueFromElement(eFile, "watched"));
                Date watchedDate;

                String stringDate = DOMHelper.getValueFromElement(eVideo, "watchedDate");
                if (StringUtils.isNumeric(stringDate)) {
                    watchedDate = new Date(Long.parseLong(stringDate));
                } else {
                    watchedDate = new Date();
                }

                for (int episode = firstPart; episode <= lastPart; episode++) {
                    episodes.add(new Episode(season, episode, watched, watchedDate));
                }
            }
        }
    }
    return episodes;
}

From source file:com.unitedcoders.android.gpodroid.tools.Tools.java

public static String getImageUrlFromFeed(Context context, String url) {

    try {//from   www  . j av  a2  s .  co  m
        Document doc;
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        doc = builder.parse(response.getEntity().getContent());
        //            return doc;

        // get Image
        NodeList item = doc.getElementsByTagName("channel");
        Element el = (Element) item.item(0);

        String imageUrl;
        NodeList imagNode = el.getElementsByTagName("image");
        if (imagNode != null) {
            Element ima = (Element) imagNode.item(0);
            if (ima != null) {
                NodeList urlNode = ima.getElementsByTagName("url");
                if (urlNode == null || urlNode.getLength() < 1)
                    imageUrl = null;
                else
                    imageUrl = urlNode.item(0).getFirstChild().getNodeValue();
            } else
                imageUrl = null;
        } else
            imageUrl = null;

        return imageUrl;

    } catch (IOException e) {
        return null; // The network probably died, just return null
    } catch (SAXException e) {
        // Problem parsing the XML, log and return nothing
        Log.e("NCRSS", "Error parsing XML", e);
        return null;
    } catch (Exception e) {
        // Anything else was probably another network problem, fail silently
        return null;
    }

}

From source file:Main.java

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

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

    try {//from  ww  w.ja  va  2  s .c o  m
        File fXmlFile = new File(path, filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList songList = doc.getElementsByTagName("song");
        Log.d("ReadItemsFromFile", "List Length: " + songList.getLength());

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

            Node nNode = songList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                HashMap<String, String> mapElement = new HashMap<String, String>();

                mapElement.put("name", eElement.getElementsByTagName("name").item(0).getTextContent());
                mapElement.put("artist", eElement.getElementsByTagName("artist").item(0).getTextContent());
                mapElement.put("startTime",
                        eElement.getElementsByTagName("startTime").item(0).getTextContent());
                mapElement.put("url", eElement.getElementsByTagName("url").item(0).getTextContent());

                results.add(mapElement);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return results;

}

From source file:Main.java

/**
 * Get string value for a tag//ww w  .  ja v  a  2 s . c  o  m
 * 
 * @param xmlElement
 *            Root Element of subtree in which required tag is to be
 *            searched
 * @param key
 *            Required tage name
 * @return String value
 */
public static String getStringValueForXMLTagFromAnywhere(Element xmlElement, String key) {

    // Start - testing
    String value = xmlElement.getAttribute(key);
    System.out.println("Value is - " + value);
    // End - testing

    NodeList nl = xmlElement.getElementsByTagName(key);
    String output = "nothing";
    if (nl.getLength() > 0) {
        Node node = nl.item(0).getFirstChild();
        if (node != null) {
            output = node.getNodeValue().trim();
            return output;
        }
    }
    return output;
}

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

/**
 * Renvoie une lment qui doit tre unique dans le document.
 * /*ww w  .ja  v  a  2  s .  c  om*/
 * @param root
 * @param elementName
 * @return
 */
public static Element getUniqueElement(Element root, String elementName) {
    NodeList list = root.getElementsByTagName(elementName);
    return (Element) list.item(0);
}

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

public static String getElementText(Element root, String elementName) {
    NodeList list = root.getElementsByTagName(elementName);
    if (list.getLength() == 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("No element named '" + elementName + "' under '" + root.getNodeName() + "'");
        }/*w  ww .j ava 2s.  c  om*/
        return null;
    }
    return getElementText((Element) list.item(0));
}