Example usage for org.w3c.dom Document getElementsByTagName

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

Introduction

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

Prototype

public NodeList getElementsByTagName(String tagname);

Source Link

Document

Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.

Usage

From source file:Main.java

private static boolean validateUserXML(Document doc) {
    NodeList nodeList = null;/*from   w w  w .  j a  v  a  2s  .com*/

    // Check the elements and values exist
    nodeList = doc.getElementsByTagName("user");
    if (nodeList.getLength() != 1) {
        return false;
    }

    // Check that email element exists
    nodeList = doc.getElementsByTagName("username");
    if (nodeList.getLength() != 1) {
        return false;
    }

    // Check that value is not null or empty
    String username = getValue((Element) doc.getElementsByTagName("user").item(0), "username");
    if (username == null || username.isEmpty()) {
        return false;
    }

    // Check that email element exists
    nodeList = doc.getElementsByTagName("password");
    if (nodeList.getLength() != 1) {
        return false;
    }

    // Check that value is not null or empty
    String password = getValue((Element) doc.getElementsByTagName("user").item(0), "password");
    if (password == null || password.isEmpty()) {
        return false;
    }

    return true;
}

From source file:Main.java

public static Element getRoot(Document document, String root) {
    if (document == null)
        throw new IllegalArgumentException("Null document");

    NodeList nodeList = document.getElementsByTagName(root);
    return nodeList.item(0) != null ? (Element) nodeList.item(0) : null;
}

From source file:Main.java

public static Node getElementByTagName(Document document, String tagName) {
    if (document == null || tagName == null) {
        return null;
    }/*from w ww .  j  a v a2  s .  c  o  m*/

    NodeList nodes = document.getElementsByTagName(tagName);
    if (nodes == null || nodes.getLength() < 1) {
        return null;
    }
    return nodes.item(0);
}

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 {/*ww  w.j a va 2s.  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

public static Map<String, Element> getMBeanAttributes(Document document, String mbeanCode) {
    Map<String, Element> attrs = new HashMap<String, Element>();

    NodeList nlist = document.getElementsByTagName("mbean");

    Element beanNode = null;//from ww  w  .  j a v  a 2 s.co m

    for (int i = 0; i < nlist.getLength(); i++) {
        Element elem = (Element) nlist.item(i);
        String attr = elem.getAttribute("code");
        if (mbeanCode.equals(attr)) {
            beanNode = elem;
            break;
        }
    }

    if (beanNode != null) {
        nlist = beanNode.getElementsByTagName("attribute");
        for (int i = 0; i < nlist.getLength(); i++) {
            Element attrElem = (Element) nlist.item(i);
            String attrName = attrElem.getAttribute("name");

            attrs.put(attrName, attrElem);
        }
    }

    return attrs;
}

From source file:Main.java

private static String getXmlString(Document document, String tagName)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {
    NodeList nodeList = document.getElementsByTagName(tagName);
    if (nodeList.getLength() < 1) {
        throw new IllegalArgumentException("no " + tagName + " found");
    }/*from  w  w w .j  a  va2  s  . c  o  m*/
    Node sub = nodeList.item(0);
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document subdoc = db.newDocument();
    subdoc.appendChild(sub.cloneNode(true));

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    DOMSource domSource = new DOMSource(subdoc);
    StringWriter sw = new StringWriter();
    StreamResult xmlResult = new StreamResult(sw);
    transformer.transform(domSource, xmlResult);
    sw.flush();
    return sw.toString();
}

From source file:Main.java

public static Element getRoot(Document document, String root) {
    if (document == null) {
        throw new IllegalArgumentException("Null document");
    }//from w  w w  .  j  a v a 2 s .  c  om

    NodeList nodeList = document.getElementsByTagName(root);
    return nodeList.item(0) != null ? (Element) nodeList.item(0) : null;
}

From source file:org.pdfsam.ui.news.NewsStage.java

static void wrapHrefToOpenNative(Document document) {
    // TODO find a better way
    NodeList nodeList = document.getElementsByTagName("a");
    for (int i = 0; i < nodeList.getLength(); i++) {
        EventTarget eventTarget = (EventTarget) nodeList.item(i);
        eventTarget.addEventListener("click", e -> {
            HTMLAnchorElement anchorElement = (HTMLAnchorElement) e.getCurrentTarget();
            eventStudio().broadcast(new OpenUrlRequest(anchorElement.getHref()));
            e.preventDefault();/* w ww .j a  va2 s.  com*/
        }, false);
    }
}

From source file:Main.java

private static boolean validateMessageXML(Document doc, String xml) {
    NodeList nodeList = null;// w  w  w. j  a  v a2 s.  c  o  m

    // Check the elements and values exist
    nodeList = doc.getElementsByTagName("message");
    if (nodeList.getLength() != 1) {
        return false;
    }

    // Check that email element exists
    nodeList = doc.getElementsByTagName("content");
    if (nodeList.getLength() != 1) {
        return false;
    }

    // Check that value is not null or empty
    String content = getValue((Element) doc.getElementsByTagName("message").item(0), "content");
    if (content == null || content.isEmpty()) {
        return false;
    }

    // Validate that user is part of the message XML
    return validateUserXML(doc);
}

From source file:Main.java

public static Document appendChildWithAttribute(Document doc, String nodeName, String parentNodeName,
        String attrName, String attrValue) {
    Element node = doc.createElement(nodeName);
    node.setAttribute(attrName, attrValue);
    doc.getElementsByTagName(parentNodeName).item(0).appendChild(node);
    return doc;/* w w w  .  ja v  a 2  s .  c om*/
}