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:com.google.wave.api.AbstractRobotServlet.java

/**
 * Parse version identifier from the capabilities.xml file, and set it to
 * {@code version} static variable.//from   ww  w . j av a2 s  .c  om
 */
private static void parseVersionIdentifier() {
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(new FileInputStream(WAVE_CAPABILITIES_XML_FILE_PATH));
        NodeList elements = document.getElementsByTagName(CAPABILITIES_XML_VERSION_TAG_NAME);
        if (elements.getLength() >= 1) {
            Node versionNode = elements.item(0);
            version = versionNode.getTextContent();
        }
    } catch (IOException e) {
        log.warning("Problem opening capabilities.xml file. Cause: " + e.getMessage());
    } catch (SAXException e) {
        log.warning("Problem parsing capabilities.xml file. Cause: " + e.getMessage());
    } catch (ParserConfigurationException e) {
        log.warning("Problem setting up XML parser. Cause: " + e.getMessage());
    }
}

From source file:com.hdsfed.cometapi.XMLHelper.java

public static Map<String, String> DocumentToMap(Document doc) {
    Map<String, String> taglistMap = new HashMap<String, String>();
    NodeList nList = doc.getElementsByTagName("tag");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                taglistMap.put(eElement.getAttribute("id"), XMLHelper.getTagValue("name", eElement) + "="
                        + XMLHelper.getTagValue("value", eElement));
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return taglistMap;
}

From source file:com.hdsfed.cometapi.XMLHelper.java

public static List<Coordinates> ExtractCoordinates(Document doc) {
    List<Coordinates> coordinateList = new ArrayList<Coordinates>();
    NodeList nList = doc.getElementsByTagName("Corner");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                coordinateList.add(new Coordinates(eElement.getAttribute("name"), getTagValue("X", eElement),
                        getTagValue("Y", eElement)));
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return coordinateList;
}

From source file:Main.java

public static Document createDocument(String mainType, String customType) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    document.setXmlStandalone(false);/*from  w ww  .j a va  2 s . co  m*/
    Element sync = document.createElement("Sync");
    document.appendChild(sync);
    Element entity = document.createElement("Entity");
    entity.setAttribute("mainType", mainType);
    if (customType.length() > 0)
        entity.setAttribute("customType", customType);
    document.getElementsByTagName("Sync").item(0).appendChild(entity);
    return document;
}

From source file:controllers.modules.cas.SecureCAS.java

public static void handleLogout(String body) throws Throwable {
    int i = StringUtils.indexOf(body, LOGOUT_REQ_PARAMETER);
    String postBody = URLDecoder.decode(body, "UTF-8");
    if (i == 0) {
        String logoutRequestMessage = StringUtils.substring(postBody, LOGOUT_REQ_PARAMETER.length() + 1);
        if (StringUtils.isNotEmpty(logoutRequestMessage)) {

            Document document = XML.getDocument(logoutRequestMessage);
            if (document != null) {
                NodeList nodeList = document.getElementsByTagName("samlp:SessionIndex");
                if (nodeList != null && nodeList.getLength() > 0) {
                    Node node = nodeList.item(0);
                    String ticket = node.getTextContent();
                    String stKey = ST + "_" + ticket;
                    String username = Cache.get(stKey, String.class);
                    if (username != null) {
                        Cache.delete(stKey);
                        Cache.set(LOGOUT_TAG + "_" + username, 1, TEN_YEARS_EXPIRATION);
                        Logger.debug("Mark that %s has been logout ", username);
                        return;
                    }//ww w.  ja  v a  2s  .  c  o  m
                }
            }
        }
    }
    Logger.warn("illegal logout message: %s", postBody);
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static Element getElement(Document doc, String tagName, boolean isRequired) throws ParseException {

    NodeList list = doc.getElementsByTagName(tagName);

    if (list.getLength() == 0 && isRequired) {
        log.error("getElement(): XML is lacking required tag <" + tagName + ">");
        throw new ParseException("XML is lacking required tag <" + tagName + ">", -1);
    }/*  w ww. j  a  va2  s. co  m*/

    if (list.getLength() > 1) {
        log.error("getElement(): XML is contains multiple <" + tagName + "> tags, expected only one");
        throw new ParseException("XML is contains multiple <" + tagName + "> tags, expected only one", -1);
    }

    Node n = list.item(0);
    if (!(n instanceof Element)) {
        log.error("getElement(): Internal error, expected Element but got Node");
        throw new ParseException("Internal error, expected Element but got Node", -1);
    }
    return (Element) n;
}

From source file:net.roboconf.agent.internal.misc.UserDataUtils.java

private static String getSpecificAttributeOfTagInXMLFile(String filePath, String tagName, String attrName)
        throws ParserConfigurationException, SAXException, IOException {

    File fXmlFile = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName(tagName);
    Node aNode = nList.item(2);// w  ww .  j a v a 2  s.  c om
    NamedNodeMap attributes = aNode.getAttributes();
    String attrValue = "";
    for (int a = 0; a < attributes.getLength(); a++) {
        Node theAttribute = attributes.item(a);
        if (attrName.equals(theAttribute.getNodeName()))
            attrValue = theAttribute.getTextContent().split(":")[0];
    }

    return attrValue;
}

From source file:com.hdsfed.cometapi.XMLHelper.java

public static String GetSimpleTagContent(Document doc, String tag) {
    tag = StringHelper.toTitleCase(tag);
    String content = "";
    NodeList nList2 = doc.getElementsByTagName(tag);
    if (nList2.getLength() == 0) {
        nList2 = doc.getElementsByTagName(tag.toUpperCase());
    }/*ww  w .j  a  va  2 s .co  m*/
    if (nList2.getLength() == 0) {
        nList2 = doc.getElementsByTagName(tag.toLowerCase());
    }
    if (nList2.getLength() == 0)
        return "";
    for (int temp = 0; temp < nList2.getLength(); temp++) {
        Node nNode2 = nList2.item(temp);
        if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode2;
            if (eElement != null) {
                content = eElement.getTextContent();
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return content;
}

From source file:org.opencastproject.loadtest.engage.Main.java

/**
 * Parse the episode xml to retrieve all of the episode ids.
 * /*  ww  w. ja  v a  2 s .  c  o  m*/
 * @param is
 *          This is the input stream representation of the xml.
 * @return A linked list of ids for the episodes on the engage server as Strings.
 **/
public static LinkedList<String> parseEpisodeXml(InputStream is) {
    LinkedList<String> episodes = new LinkedList<String>();
    try {
        // Parse document
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document document = docBuilder.parse(is);

        // Process results
        Node result = null;
        Node id = null;

        for (int i = 0; i < document.getElementsByTagName("result").getLength(); i++) {
            result = document.getElementsByTagName("result").item(i);
            id = result.getAttributes().getNamedItem("id");
            episodes.add(id.getNodeValue());
        }
    } catch (ParserConfigurationException pce) {
        logger.error(
                "The list of episodes could not be parsed from the xml received from the engage server because of:",
                pce);
    } catch (IOException ioe) {
        logger.error(
                "The list of episodes could not be parsed from the xml received from the engage server because of:",
                ioe);
    } catch (SAXException sae) {
        logger.error(
                "The list of episodes could not be parsed from the xml received from the engage server because of:",
                sae);
    }

    return episodes;
}

From source file:com.sinet.gage.dlap.utils.XMLUtils.java

/**
 * @param String//w w w  .  j a  v a2s  .  c om
 * @param String
 * @return String
 */
public static String parseXML(String xml, String rootElementName) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db;
        db = dbf.newDocumentBuilder();
        Document newDoc = db.parse(new ByteArrayInputStream(xml.getBytes()));

        Element element = (Element) newDoc.getElementsByTagName(rootElementName).item(0);
        // Imports a node from another document to this document,
        // without altering
        Document newDoc2 = db.newDocument();
        // or removing the source node from the original document
        Node copiedNode = newDoc2.importNode(element, true);
        // Adds the node to the end of the list of children of this node
        newDoc2.appendChild(copiedNode);

        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");

        Writer out = new StringWriter();

        tf.transform(new DOMSource(newDoc2), new StreamResult(out));

        return out.toString();
    } catch (TransformerException | ParserConfigurationException | SAXException | IOException e) {
        LOGGER.error("Exception in parsing xml from response: ", e);
    }
    return "";
}