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:edu.harvard.i2b2.eclipse.plugins.query.utils.XmlUtil.java

public static Boolean hasPatientCountTag(XmlValueType xml) {
    Element rootElement = xml.getAny().get(0);
    // <name>PATIENTSET</name>
    NodeList pcElements = rootElement.getElementsByTagName("name");
    if (pcElements.getLength() == 0)
        return false;
    else {/*from   w w  w .  j a  va2s. c om*/
        Boolean result = false;
        for (int i = 0; i < pcElements.getLength(); i++) {
            String resultTypeName = pcElements.item(i).getTextContent();
            //      log.info(resultTypeName);
            if (resultTypeName.equals("PATIENT_COUNT_XML")) {
                result = true;
            }
        }
        return result;
    }
}

From source file:edu.harvard.i2b2.eclipse.plugins.query.utils.XmlUtil.java

public static Boolean hasEncounterSetTag(XmlValueType xml) {
    Element rootElement = xml.getAny().get(0);
    // <name>PATIENTSET</name>
    NodeList psElements = rootElement.getElementsByTagName("name");
    if (psElements.getLength() == 0)
        return false;
    else {/*from  w  w w  . j av  a 2 s .com*/
        Boolean result = false;
        for (int i = 0; i < psElements.getLength(); i++) {
            String resultTypeName = psElements.item(i).getTextContent();
            //      log.info(resultTypeName);
            if (resultTypeName.equals("PATIENT_ENCOUNTER_SET")) {
                result = true;
            }
        }
        return result;
    }
}

From source file:edu.harvard.i2b2.eclipse.plugins.query.utils.XmlUtil.java

public static Boolean hasBreakdownTag(XmlValueType xml) {
    Element rootElement = xml.getAny().get(0);
    // <name>PATIENTSET</name>
    NodeList psElements = rootElement.getElementsByTagName("name");
    if (psElements.getLength() == 0)
        return false;
    else {//from  w  w w.java  2  s  . c o m
        Boolean result = false;
        for (int i = 0; i < psElements.getLength(); i++) {
            String resultTypeName = psElements.item(i).getTextContent();
            //      log.info(resultTypeName);
            if (resultTypeName.equals("PATIENT_GENDER_COUNT_XML")
                    || resultTypeName.equals("PATIENT_AGE_COUNT_XML")
                    || resultTypeName.equals("PATIENT_VITALSTATUS_COUNT_XML")
                    || resultTypeName.equals("PATIENT_RACE_COUNT_XML")) {
                result = true;
            }
        }
        return result;
    }
}

From source file:Main.java

/**
 * Wrapper for <code>getElementsByTagName</code> with the "quirk" that it
 * returns an array instead of a "live" NodeList (meaning that changes to
 * the DOM tree after returning the element list will not affect the element
 * list).//from   w w  w . j av a 2  s .  co  m
 * 
 * @param parent
 * @param decendantElementsNS
 * @param decentantElementsName
 * @return
 */
public static List<Element> getNamedDecendants(Element parent, String decendantElementsNS,
        String... decentantElementsNames) {
    List<Element> res = new ArrayList<Element>();
    for (String decentantElementsName : decentantElementsNames) {
        NodeList elements = null;
        if (decendantElementsNS != null && decendantElementsNS.length() > 0) {
            elements = parent.getElementsByTagNameNS(decendantElementsNS, decentantElementsName);
        } else {
            elements = parent.getElementsByTagName(decentantElementsName);
        }

        for (int i = 0; i < elements.getLength(); i++) {
            res.add((Element) elements.item(i));
        }
    }
    return res;
}

From source file:com.ibm.bi.dml.conf.DMLConfig.java

/**
 * Method to get the string value of an element identified by tag
 * @param ele//from   w ww.jav  a2 s.com
 * @param tagName
 * @return
 */
private static String getTextValue(Element element, String tagName) {
    String textVal = null;
    NodeList list = element.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        Element elem = (Element) list.item(0);
        textVal = elem.getFirstChild().getNodeValue();

    }
    return textVal;
}

From source file:com.concursive.connect.web.portal.ProjectPortalUtils.java

public static DashboardPage retrieveDashboardPage(ProjectPortalBean portalBean) throws Exception {

    // Use the project-portal-config.xml to determine the dashboard page to use for the current url
    URL resource = ProjectPortalUtils.class.getResource("/portal/project-portal-config.xml");
    LOG.debug("portal config file: " + resource.toString());
    XMLUtils library = new XMLUtils(resource);

    // The nodes are listed under the <navigation> tag
    Element portal = XMLUtils.getFirstChild(library.getDocumentElement(), "portal", "type", "navigation");

    String moduleLayout = null;//w  w  w  .j a  v  a  2  s  . c o m
    String page = null;
    String modulePermission = null;

    // Look through the modules
    NodeList moduleNodeList = portal.getElementsByTagName("module");
    for (int i = 0; i < moduleNodeList.getLength(); i++) {
        Node thisModule = moduleNodeList.item(i);

        NodeList urlNodeList = ((Element) thisModule).getElementsByTagName("url");
        for (int j = 0; j < urlNodeList.getLength(); j++) {
            Element urlElement = (Element) urlNodeList.item(j);

            // Construct a ProjectPortalURL and add to the list for retrieving
            String name = urlElement.getAttribute("name");

            // <url name="/show" object="/" page="project profile"/>
            if (name.equals("/" + portalBean.getAction())) {
                String object = urlElement.getAttribute("object");
                if (object.equals("/" + portalBean.getDomainObject())) {
                    // Set the module for tab highlighting
                    portalBean.setModule(((Element) thisModule).getAttribute("name"));

                    // Set the page for looking up
                    page = urlElement.getAttribute("page");

                    // Set the layout filename to find the referenced page
                    moduleLayout = ((Element) thisModule).getAttribute("layout");

                    // Set the permission required for viewing this page
                    modulePermission = ((Element) thisModule).getAttribute("permission");
                    break;
                }
            }
        }
    }

    URL moduleResource = ProjectPortalUtils.class.getResource("/portal/" + moduleLayout);
    LOG.debug("module config file: " + moduleResource.toString());
    XMLUtils module = new XMLUtils(moduleResource);

    Element moduleElement = XMLUtils.getFirstChild(module.getDocumentElement(), "navigation");

    NodeList pageNodeList = moduleElement.getElementsByTagName("page");
    if (pageNodeList.getLength() == 0) {
        LOG.warn("No pages found in moduleLayout: " + moduleLayout);
    }

    for (int j = 0; j < pageNodeList.getLength(); j++) {
        Node thisPage = pageNodeList.item(j);

        String pageName = ((Element) thisPage).getAttribute("name");

        if (pageName.equals(page)) {
            LOG.debug("Found page " + pageName);

            // Find a portal template to use
            Project thisProject = ProjectUtils.loadProject(portalBean.getProjectId());
            DashboardPage dashboardPage = null;
            if (thisProject.getCategoryId() > -1) {
                // Use a category specific portal
                ProjectCategory category = ProjectUtils.loadProjectCategory(thisProject.getCategoryId());
                LOG.debug("Trying a category specific portal: " + page + ": "
                        + category.getDescription().toLowerCase());
                dashboardPage = DashboardUtils.loadDashboardPage(DashboardTemplateList.TYPE_NAVIGATION,
                        (portalBean.isPopup() ? page + "-popup" : page) + ": "
                                + category.getDescription().toLowerCase(),
                        moduleLayout);
            }
            if (dashboardPage == null) {
                LOG.debug("Trying a specific portal: " + page);
                dashboardPage = DashboardUtils.loadDashboardPage(DashboardTemplateList.TYPE_NAVIGATION,
                        (portalBean.isPopup() ? page + "-popup" : page), moduleLayout);
            }
            if (dashboardPage == null) {
                LOG.warn("Page not found: " + page);
                System.out.println("ProjectPortalUtils-> * PAGE NOT FOUND *");
                return null;
            }
            dashboardPage.setProjectId(thisProject.getId());
            // Set the dashboard page's module permission if it doesn't have a page permission
            if (dashboardPage.getPermission() == null) {
                dashboardPage.setPermission(modulePermission);
            }
            return dashboardPage;
        }
    }
    return null;
}

From source file:eu.europeana.uim.sugarcrmclient.internal.helpers.ClientUtils.java

/**
 * Auxiliary method for extracting information from DOM objects embedded in
 * SOAP responses//w  w w  .  j av a  2s  .co  m
 * 
 * @param value
 *            The value to be extracted
 * @param el
 *            the DOM element
 * @return the value
 */
public static String extractFromElement(String value, Element el) {
    NodeList nl = el.getElementsByTagName("item");

    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        String textcontent = nd.getChildNodes().item(0).getTextContent();
        if (value.equals(textcontent)) {
            return nd.getChildNodes().item(1).getTextContent();
        }

    }
    return null;
}

From source file:client.QueryLastFm.java

License:asdf

public static void findSimilarTracks(PrintWriter track_id_out, String sourceMbid, String trackName,
        String artistName) throws Exception {
    String destMbid;//from   w  ww  . ja  v  a  2 s .  co m
    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {
        Thread.sleep(50); //1000 milliseconds is one second.
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    try {
        URI uri = new URIBuilder().setScheme("http").setHost("ws.audioscrobbler.com").setPath("/2.0/")
                .setParameter("method", "track.getsimilar").setParameter("artist", artistName)
                .setParameter("track", trackName).setParameter("limit", "10")
                .setParameter("api_key", "88858618961414f8bec919bddd057044").build();

        // new URIBuilder().
        HttpGet request = new HttpGet(uri);

        // request.
        // This is useful for last.fm logging and preventing them from blocking this client
        request.setHeader(HttpHeaders.USER_AGENT,
                "nileshmore@gatech.edu - ClassAssignment at GeorgiaTech Non-commercial use");

        CloseableHttpResponse response = httpclient.execute(request);

        int statusCode = response.getStatusLine().getStatusCode();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        try {
            if (statusCode == 200) {
                HttpEntity entity1 = response.getEntity();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader((response.getEntity().getContent())));
                Document document = builder.parse((response.getEntity().getContent()));
                Element root = document.getDocumentElement();
                root.normalize();

                NodeList mbidList = root.getElementsByTagName("mbid");
                // System.out.println("mbid" + mbidList.getLength());

                for (int n = 0; n < mbidList.getLength(); n++) {
                    Node attribute = mbidList.item(n);
                    if (mbidList.item(n).hasChildNodes()) {
                        if (mbidList.item(n).getParentNode().getNodeName().matches("track")) // to get correct mbid
                        {
                            destMbid = mbidList.item(n).getFirstChild().getNodeValue();
                            // track_id_out.print(sourceMbid);
                            // track_id_out.print(",");
                            // track_id_out.println(destMbid);
                            if (isAlreadyInserted(sourceMbid, destMbid)) //  if not inserted , insert into map
                            {
                                // System.out.println(sourceMbid + "---"  + destMbid);
                                // track_id_out.print(sourceMbid);
                                // track_id_out.print(",");
                                // track_id_out.println(destMbid);
                                // continue;
                            }
                            /*if(isAlreadyInserted(sourceMbid, destMbid))
                            {
                              System.out.println("Ok got the match !!");
                            }*/
                            else {
                                track_id_out.print(sourceMbid);
                                track_id_out.print(",");
                                track_id_out.println(destMbid); //
                                // track_id_out.print(",");
                                // track_id_out.println("Undirected");
                            }
                            // track_id_out.print()
                        }
                    }
                }

            }

        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

}

From source file:eu.eidas.auth.engine.configuration.dom.DOMConfigurationParser.java

/**
 * Generate parameters./*  w ww  . j  a v a2  s. c  om*/
 *
 * @param configurationTag the configuration node
 * @return the map< string, string>
 */
@Nonnull
private static ImmutableMap<String, String> parseParameters(@Nonnull String configurationFileName,
        @Nonnull String instanceName, @Nonnull String configurationName, @Nonnull Element configurationTag)
        throws SamlEngineConfigurationException {
    Map<String, String> parameters = new LinkedHashMap<String, String>();

    NodeList parameterTags = configurationTag
            .getElementsByTagName(InstanceTag.ConfigurationTag.ParameterTag.TAG_NAME);

    for (int i = 0, n = parameterTags.getLength(); i < n; i++) {
        // for every parameter find, process.
        Element parameterTag = (Element) parameterTags.item(i);
        String parameterName = parameterTag
                .getAttribute(InstanceTag.ConfigurationTag.ParameterTag.Attribute.NAME);
        String parameterValue = parameterTag
                .getAttribute(InstanceTag.ConfigurationTag.ParameterTag.Attribute.VALUE);

        // verify the content.
        if (StringUtils.isBlank(parameterName)) {
            String message = "SAML engine configuration file \"" + configurationFileName
                    + "\" contains a blank parameter name for configuration name \"" + configurationName
                    + "\" in instance name \"" + instanceName + "\"";
            LOG.error(message);
            throw new SamlEngineConfigurationException(message);
        }
        if (StringUtils.isBlank(parameterValue)) {
            String message = "SAML engine configuration file \"" + configurationFileName
                    + "\" contains parameter name \"" + parameterName
                    + "\" with a blank value for configuration name \"" + configurationName
                    + "\" in instance name \"" + instanceName + "\"";
            LOG.error(message);
            throw new SamlEngineConfigurationException(message);
        }
        String previous = parameters.put(parameterName.trim(), parameterValue.trim());
        if (null != previous) {
            String message = "Duplicate parameter entry names \"" + parameterName
                    + "\" in SAML engine configuration file \"" + configurationFileName
                    + "\" for configuration name \"" + configurationName + "\" in instance name \""
                    + instanceName + "\"";
            LOG.error(message);
            throw new SamlEngineConfigurationException(message);
        }
    }
    return ImmutableMap.copyOf(parameters);
}

From source file:Main.java

/**
 * Get textvalue from path or attribute. This is 
 * called by other getText() methods. Endusers usually
 * should not call this directly./*from   w  w  w.  j  a  v  a  2  s . co  m*/
 * @param element
 * @param path
 * @param pathOffset
 * @return
 */
public static String getText(Element element, String[] path, int pathOffset) {
    int len = path.length;
    boolean isAttrText = path[len - 1].charAt(0) == '@';
    if (isAttrText)
        len--; // last item is @attribute identifier

    // start path from given offset index
    for (int i = pathOffset; i < len; i++) {
        if (element == null)
            return null;
        NodeList nodes = element.getElementsByTagName(path[i]);
        element = (Element) nodes.item(0);
    }

    if (isAttrText) {
        if (element == null)
            return null;
        Attr attr = element.getAttributeNode(path[len].substring(1));
        return (attr != null ? attr.getValue() : null);
    } else {
        return getSimpleText(element);
    }
}