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:net.sf.commons.ssh.directory.Directory.java

private Map<String, Description> load() throws ParserConfigurationException, SAXException, IOException {
    final Document directoryDocument = getDocument("directory.xml");
    final NodeList factories = directoryDocument.getElementsByTagName("factory"); //$NON-NLS-1$

    //sort by priority
    SortedSet<Description> factoriesSet = new TreeSet<Description>(new Comparator<Description>() {
        @Override/*w  w  w .jav a2 s  .c  o m*/
        public int compare(Description o1, Description o2) {
            return o1.getPriority().compareTo(o2.getPriority());
        }
    });
    for (int i = 0; i < factories.getLength(); i++) {
        final Element element = (Element) factories.item(i);
        final Description description = Description.loadDescription(element);
        factoriesSet.add(description);
    }

    final Map<String, Description> result = new LinkedHashMap<String, Description>(factories.getLength());
    for (Description description : factoriesSet) {
        result.put(description.getClassName(), description);
    }
    return result;
}

From source file:net.sf.commons.ssh.directory.Directory.java

private Map<String, String> loadOldClassNames() throws IOException, SAXException, ParserConfigurationException {
    final Document directoryDocument = getDocument("factory-compatibility.xml");
    final NodeList classNames = directoryDocument.getElementsByTagName("factory"); //$NON-NLS-1$
    HashMap<String, String> result = new HashMap<String, String>(classNames.getLength());
    for (int i = 0; i < classNames.getLength(); i++) {
        final Element element = (Element) classNames.item(i);
        result.put(getFromElement(element, "compat-class-name", true),
                getFromElement(element, "class-name", true));
    }/*from w  w w  . j  a va2 s . c o  m*/
    return result;
}

From source file:WSpatern.SubFolderWS.java

private void parseXML(String line) {
    sub_folder.clear();/*from w  w w  . j  av  a  2 s  . c  o  m*/
    sub_folder_Id.clear();
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("dirLink");
        if (response.getLength() > 0) {
            for (int i = 0; i < response.getLength(); i++) {
                Element err = (Element) response.item(i);
                sub_folder.add(err.getElementsByTagName("name").item(0).getTextContent());
                sub_folder_Id.add(err.getElementsByTagName("id").item(0).getTextContent());
                ///   System.out.println("Folder "+"-->" +sub_folder);
            }

        }
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(LoginWS.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXException ex) {
        Logger.getLogger(LoginWS.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LoginWS.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.apache.streams.gnip.powertrack.ActivityXMLActivitySerializer.java

private String getValueFromXML(String tag, Document doc) throws Exception {
    Element base = (Element) doc.getElementsByTagName("entry").item(0);
    return base.getElementsByTagName(tag).item(0).getTextContent();
}

From source file:de.uni_potsdam.hpi.bpt.promnicat.importer.sap_rm.SapReferenceModelImporter.java

public void splitEPML(File file)
        throws ParserConfigurationException, SAXException, IOException, TransformerException, JSONException {
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = fac.newDocumentBuilder();
    Document doc = builder.parse(file);
    NodeList epcNodes = doc.getElementsByTagName("epc");
    for (int i = 0; i < epcNodes.getLength(); i++) {
        String epcName = epcNodes.item(i).getAttributes().getNamedItem("name").getNodeValue();

        Model model = this.persistenceApi.loadCompleteModelWithImportedId(epcName);
        if (model != null) {
            continue;
        }//from w w w . jav  a  2s  . co m

        Document newEpcDoc = builder.newDocument();
        Element epml = newEpcDoc.createElement("epml:epml");
        Element directory = newEpcDoc.createElement("directory");
        newEpcDoc.appendChild(epml);
        epml.appendChild(directory);
        directory.setAttribute("name", "ROOT");
        Node epcNode = newEpcDoc.importNode(epcNodes.item(i), true);
        directory.appendChild(epcNode);

        DOMSource epmlSource = new DOMSource(newEpcDoc);
        final File epml2eRDFxsltFile = new File("resources/xslt/EPML2eRDF.xslt");
        final Source epml2eRDFxsltSource = new StreamSource(epml2eRDFxsltFile);

        // Transformer Factory
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();

        // Get the epml source
        Transformer transformer = transformerFactory.newTransformer(epml2eRDFxsltSource);
        StringWriter writer = new StringWriter();
        transformer.transform(epmlSource, new StreamResult(writer));
        String erdf = writer.toString();
        String rdf = erdfToRdf(erdf);
        rdf = rdf.replaceAll("ns[0-9]+:", "");
        rdf = rdf.replace("#resource", "#");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document rdfDoc = builder.parse(new ByteArrayInputStream(rdf.getBytes("UTF-8")));
        String json = RdfJsonTransformation.toJson(rdfDoc, "").toString();

        //Diagram diagram = DiagramBuilder.parseJson(json);
        Representation representation = new Representation(Constants.FORMAT_BPMAI_JSON, Constants.NOTATION_EPC);
        Diagram parseJson = DiagramBuilder.parseJson(json);
        representation.setDataContent(json.getBytes());
        model = new Model(epcName, Constants.ORIGIN_SAP_RM);
        model.setImportedId(epcName);
        Revision revision = new Revision(0);
        revision.connectRepresentation(representation);
        model.connectLatestRevision(revision);
        revision.connectModel(model);
        model.connectLatestRevision(revision);
        persistenceApi.savePojo(model);

    }

}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi Gets values of items in repeatable container for a given
 * repetition//from www. j ava2  s .  co m
 *
 * @param xmlContent xml message
 * @param repContainerName name of the repeatables container
 * @param repeatables List of FormField contained in the repetition
 * @param repetitionNumber the repetition instance (it starts from 0 not
 * from 1)
 * @return List of values for the given repetition
 * @throws Exception
 */
public static List<String> getValuesForRepetition(String xmlContent, String repContainerName,
        List<FormField> repeatables, int repetitionNumber) throws Exception {
    List<String> ret = new ArrayList<String>();
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    NodeList repetitionList = xmlDoc.getElementsByTagName(repContainerName);
    Node givenRepetition = null;
    try {
        givenRepetition = repetitionList.item(repetitionNumber);
    } catch (Exception e) {
        for (FormField rep : repeatables) {
            ret.add("");
        }
        return ret;
    }
    Element repElement = (Element) givenRepetition;
    for (int i = 0; i < repeatables.size(); i++) {
        if (repeatables.get(i).getType() == FormFieldType.CURRENCY_FIELD) {
            String nodename = repeatables.get(i).getName() + "_curr";
            try {
                String currency = repElement.getElementsByTagName(nodename).item(0).getTextContent();
                ret.add(repElement.getElementsByTagName(repeatables.get(i).getName()).item(0).getTextContent()
                        + currency);
            } catch (Exception e) {
                ret.add("");
            }

        } else {
            try {
                String value = repElement.getElementsByTagName(repeatables.get(i).getName()).item(0)
                        .getTextContent();
                if (repeatables.get(i).getType() == FormFieldType.CHECK_BOX && value.equals("")) {
                    ret.add("false");
                } else {
                    ret.add(repElement.getElementsByTagName(repeatables.get(i).getName()).item(0)
                            .getTextContent());
                }
            } catch (Exception e) {
                ret.add("");
            }
        }
    }
    return ret;
}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi Gets values of items in repeatable container in a
 * certain survey value section//from   www.ja v a  2s .com
 *
 * @param xmlContent xml message
 * @param repContainerName name of the repeatables container
 * @param surveySection name of the current survey section
 * @param repeatables List of FormField contained in the repetition
 * @return List of values for the given repetition (identified by surver
 * section name)
 * @throws Exception
 */
public static List<String> getValuesForRepetitionWithSurvey(String xmlContent, String repContainerName,
        String surveySection, List<FormField> repeatables) throws Exception {
    List<String> ret = new ArrayList<String>();
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    NodeList repetitionNodeList = xmlDoc.getElementsByTagName(repContainerName);
    if (repetitionNodeList.getLength() == 0) { // check if the repetition
        // exissts
        for (FormField ff : repeatables) {
            ret.add("");
        }
        return ret;
    }
    Element repetitionNode = (Element) xmlDoc.getElementsByTagName(repContainerName).item(0);
    Element surveyNode = (Element) repetitionNode.getElementsByTagName(surveySection).item(0);
    for (FormField ff : repeatables) {
        if (ff.getType() == FormFieldType.CURRENCY_FIELD) {
            String nodename = ff.getName() + "_curr";
            try {
                String currency = surveyNode.getElementsByTagName(nodename).item(0).getTextContent();
                ret.add(surveyNode.getElementsByTagName(ff.getName()).item(0).getTextContent() + currency);
            } catch (Exception e) {
                ret.add("");
            }
        } else {
            try {
                String value = surveyNode.getElementsByTagName(ff.getName()).item(0).getTextContent();
                if (ff.getType() == FormFieldType.CHECK_BOX && value.equals("")) {
                    ret.add("false");
                } else {
                    ret.add(surveyNode.getElementsByTagName(ff.getName()).item(0).getTextContent());
                }
            } catch (Exception e) {
                ret.add("");
            }
        }
    }
    return ret;

}

From source file:net.neurowork.cenatic.centraldir.workers.xml.CapacidadXmlImporter.java

private Capacidad importCapacidad(String xmlString, Integer capacityId)
        throws ParserConfigurationException, SAXException, IOException {
    Document doc = XmlParserUtil.createDocumentFromString(xmlString);
    Capacidad ret = null;/*from   ww  w  .  j  a  v a  2 s . co  m*/
    NodeList nodeLst = doc.getElementsByTagName("capacity");

    for (int s = 0; s < nodeLst.getLength(); s++) {
        Node fstNode = nodeLst.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            Element elPDU = (Element) fstNode;
            String code = XmlParserUtil.getAttribute(elPDU, XML_CODE);
            String category = XmlParserUtil.getAttribute(elPDU, "category");
            NodeList fstNm = elPDU.getChildNodes();
            String capacidadName = null;

            if (fstNm.getLength() > 0) {
                capacidadName = ((Node) fstNm.item(0)).getNodeValue();

                Integer capId = AbstractXmlImporter.getId(code);
                Capacidad capacidad = null;
                try {
                    List<Capacidad> capacidades = organizacionService.findCapacidadByName(capacidadName);

                    if (capacidades != null && capacidades.size() > 0) {
                        capacidad = capacidades.get(0);
                    } else {
                        capacidad = new Capacidad();
                        capacidad.setName(capacidadName);
                        if (StringUtils.hasLength(category))
                            capacidad.setCategoria(category);
                        organizacionService.saveCapacidad(capacidad);
                    }

                    if (capId != null && capId.equals(capacityId)) {
                        ret = capacidad;
                    }
                } catch (ServiceException e) {
                    logger.error(e.getMessage());
                }
            }
        }
    }

    if (ret != null) {
        if (logger.isTraceEnabled())
            logger.trace("Se devuelve la Capacidad: " + ret);
        return ret;
    }
    if (logger.isTraceEnabled())
        logger.trace("No se ha encontrado la Capacidad con Id: " + capacityId);
    return null;
}

From source file:net.neurowork.cenatic.centraldir.workers.XMLRestWorker.java

public static String parseToken(String xmlString)
        throws ParserConfigurationException, SAXException, IOException {
    Document doc = XmlParserUtil.createDocumentFromString(xmlString);
    String retToken = null;/*w  w w. j a  va 2s  .c  o m*/

    NodeList nodeLst = doc.getElementsByTagName(TOKEN_RESULT_TAG);
    for (int s = 0; s < nodeLst.getLength(); s++) {
        Node fstNode = nodeLst.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            Element elPDU = (Element) fstNode;
            Element tokenEl = XmlParserUtil.getChildren(elPDU, TOKEN_TAG);
            retToken = XmlParserUtil.getAttribute(tokenEl, CODE_ATTR);
        }
    }
    logger.info("Token: " + retToken);
    return retToken;
}

From source file:org.jetbrains.webdemo.help.HelpLoader.java

private void generateHelpForWords() {
    resultWords = new ArrayNode(JsonNodeFactory.instance);
    try {/*from   ww w . j  a  v  a 2s.co  m*/
        File file = new File(
                CommonSettings.HELP_DIRECTORY + File.separator + ApplicationSettings.HELP_FOR_WORDS);
        Document doc = ResponseUtils.getXmlDocument(file);
        if (doc == null) {
            return;
        }
        NodeList nodeList = doc.getElementsByTagName("keyword");

        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {

                Element element = (Element) node;
                ObjectNode jsonObject = resultWords.addObject();
                jsonObject.put("word", getTagValue("name", element));
                jsonObject.put("help", getTagValueWithInnerTags("text", element));
            }
        }
    } catch (Exception e) {
        ErrorWriter.ERROR_WRITER.writeExceptionToExceptionAnalyzer(e,
                SessionInfo.TypeOfRequest.LOAD_EXAMPLE.name(), "unknown", "");
    }
    response.append("\nHelp for keywords was loaded.");
}