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.krawler.esp.handlers.basecampHandler.java

public static String importProjectFromBaseCamp(String userMap, String cid, String lid, String docid,
        String lName) {//from w w w  .  j a  v  a 2 s.c o  m
    String result = "";
    String projectName = "MyProject";
    String[] projectNames = { "MyProject", "MyProject1" };
    JSONArray jobj = null;
    Connection conn = null;
    Locale locale = Locale.ENGLISH;
    try {
        jobj = new JSONArray(userMap);
    } catch (JSONException ex) {
    }
    try {
        String destinationDirectory = StorageHandler.GetDocStorePath() + StorageHandler.GetFileSeparator()
                + "baseCamxml";
        conn = DbPool.getConnection();
        CompanyDAO cd = new CompanyDAOImpl();
        locale = cd.getCompanyLocale(conn, cid);
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder
                .parse(new File(destinationDirectory + StorageHandler.GetFileSeparator() + docid + ".xml"));
        doc.getDocumentElement().normalize();
        NodeList projects = doc.getElementsByTagName("projects");
        NodeList projectList = ((Element) projects.item(0)).getElementsByTagName("project");
        for (int j = 0; j < projectList.getLength(); j++) {
            Element project = (Element) projectList.item(j);
            String projName = project.getElementsByTagName("name").item(0).getChildNodes().item(0)
                    .getNodeValue();
            String created_on = project.getElementsByTagName("created-on").item(0).getChildNodes().item(0)
                    .getNodeValue();
            project.getElementsByTagName("last-changed-on").item(0).getChildNodes().item(0).getNodeValue();
            String status = project.getElementsByTagName("status").item(0).getChildNodes().item(0)
                    .getNodeValue();

            String projid = createProj(conn, cid, projName, created_on, status);

            NodeList userList = ((Element) project.getElementsByTagName("participants").item(0))
                    .getElementsByTagName("person");
            for (int userCount = 0; userCount < userList.getLength(); userCount++) {
                JSONObject user = getSystemUser(jobj,
                        userList.item(userCount).getChildNodes().item(0).getNodeValue());
                if (user != null && !user.getString("userid").equals("")) {
                    createresource(conn, projid, user.getString("userid"), user.getString("username"), 3);
                }
            }
            createresource(conn, projid, lid, lName, 4);

            project.getElementsByTagName("milestones").item(0).getChildNodes().item(0).getNodeValue();
            NodeList milestoneList = ((Element) project.getElementsByTagName("milestones").item(0))
                    .getElementsByTagName("milestone");

            for (int milesNo = 0; milesNo < milestoneList.getLength(); milesNo++) {
                Element milestone = (Element) milestoneList.item(milesNo);
                //                          String completedStatus = milestone.getElementsByTagName("completed").item(0).getChildNodes().item(0).getNodeValue();
                //                          if(StringUtil.stringCompareInLowercase(completedStatus,"true")){
                //                              String completed_on = milestone.getElementsByTagName("completed-on").item(0).getChildNodes().item(0).getNodeValue();
                //                              String completer_id = milestone.getElementsByTagName("completed-on").item(0).getChildNodes().item(0).getNodeValue();
                //                          }
                String title = milestone.getElementsByTagName("title").item(0).getChildNodes().item(0)
                        .getNodeValue();
                String mileStoneCreated_on = milestone.getElementsByTagName("created-on").item(0)
                        .getChildNodes().item(0).getNodeValue();
                String deadline = milestone.getElementsByTagName("deadline").item(0).getChildNodes().item(0)
                        .getNodeValue();
                String resourceid = milestone.getElementsByTagName("responsible-party-id").item(0)
                        .getChildNodes().item(0).getNodeValue();
                //                              milestone.getElementsByTagName("responsible-party-type").item(0).getChildNodes().item(0).getNodeValue();
                //                              milestone.getElementsByTagName("wants-notification").item(0).getChildNodes().item(0).getNodeValue();
                NodeList commentList = ((Element) milestone.getElementsByTagName("comments").item(0))
                        .getElementsByTagName("comment");
                String commentBody = "";
                for (int commentcout = 0; commentcout < commentList.getLength(); commentcout++) {
                    Element comment = (Element) commentList.item(commentcout);
                    //                                   comment.getElementsByTagName("attachments-count").item(0).getChildNodes().item(0).getNodeValue();
                    //                                   comment.getElementsByTagName("author-id").item(0).getChildNodes().item(0).getNodeValue();
                    commentBody = commentBody + comment.getElementsByTagName("body").item(0).getChildNodes()
                            .item(0).getNodeValue() + "<br>";
                    //                                   comment.getElementsByTagName("commentable-id").item(0).getChildNodes().item(0).getNodeValue();
                    //                                   comment.getElementsByTagName("commentable-type").item(0).getChildNodes().item(0).getNodeValue();
                    //                                   comment.getElementsByTagName("created-at").item(0).getChildNodes().item(0).getNodeValue();
                    //    //                                   comment.getElementsByTagName("emailed-from").item(0).getChildNodes().item(0).getNodeValue();
                    //    //                                   commentable-id
                }
                createTask(conn, cid, projid, title, mileStoneCreated_on, deadline, resourceid, commentBody,
                        milesNo, jobj);
                //                              body

            }
            //                         todo-lists
            NodeList todo_lists = ((Element) project.getElementsByTagName("todo-lists").item(0))
                    .getElementsByTagName("todo-list");
            for (int todosNo = 0; todosNo < todo_lists.getLength(); todosNo++) {
                Element todoList = (Element) todo_lists.item(todosNo);
                todoList.getElementsByTagName("completed-count").item(0).getChildNodes().item(0).getNodeValue();
                String des = todoList.getElementsByTagName("description").item(0).getChildNodes().item(0)
                        .getNodeValue();
                //                             todoList.getElementsByTagName("milestone-id").item(0).getChildNodes().item(0).getNodeValue();
                String name = todoList.getElementsByTagName("name").item(0).getChildNodes().item(0)
                        .getNodeValue();
                todoList.getElementsByTagName("position").item(0).getChildNodes().item(0).getNodeValue();
                todoList.getElementsByTagName("tracked").item(0).getChildNodes().item(0).getNodeValue();
                todoList.getElementsByTagName("uncompleted-count").item(0).getChildNodes().item(0)
                        .getNodeValue();
                String toDoStatus = todoList.getElementsByTagName("complete").item(0).getChildNodes().item(0)
                        .getNodeValue();

                //NodeList todo_items = todoList.getElementsByTagName("todo-items");
                //                             Connection conn,String des,String parentid,JSONArray userMap,String status,boolean group,String assignedTo,String userid
                String assignedTo = "";
                String parentid = createTodo(conn, name, des, "", jobj, toDoStatus, false, assignedTo, lid,
                        projid);
                NodeList todo_items = ((Element) todoList.getElementsByTagName("todo-items").item(0))
                        .getElementsByTagName("todo-item");

                for (int todoitemsNo = 0; todoitemsNo < todo_items.getLength(); todoitemsNo++) {
                    Element todoitem = (Element) todo_items.item(todoitemsNo);
                    toDoStatus = todoitem.getElementsByTagName("completed").item(0).getChildNodes().item(0)
                            .getNodeValue();
                    des = todoitem.getElementsByTagName("content").item(0).getChildNodes().item(0)
                            .getNodeValue();
                    todoitem.getElementsByTagName("created-on").item(0).getChildNodes().item(0).getNodeValue();
                    todoitem.getElementsByTagName("position").item(0).getChildNodes().item(0).getNodeValue();
                    todoitem.getElementsByTagName("todo-list-id").item(0).getChildNodes().item(0)
                            .getNodeValue();
                    if (todoitem.getElementsByTagName("responsible-party-id").item(0) != null) {
                        assignedTo = todoitem.getElementsByTagName("responsible-party-id").item(0)
                                .getChildNodes().item(0).getNodeValue();
                    }
                    createTodo(conn, des, des, parentid, jobj, toDoStatus, true, assignedTo, lid, projid);
                }
            }

            NodeList posts = ((Element) project.getElementsByTagName("posts").item(0))
                    .getElementsByTagName("post");
            //                         topic_id, group_id, topic_title, topic_poster, post_time, post_subject,post_text, ifread,flag

            for (int postNo = 0; postNo < posts.getLength(); postNo++) {
                Element singlePost = (Element) posts.item(postNo);
                String PostAuthId = singlePost.getElementsByTagName("author-id").item(0).getChildNodes().item(0)
                        .getNodeValue();
                String PostBody = "";
                if (singlePost.getElementsByTagName("body").item(0).getChildNodes().item(0) != null) {
                    PostBody = singlePost.getElementsByTagName("body").item(0).getChildNodes().item(0)
                            .getNodeValue();
                }
                String PostCommentCount = singlePost.getElementsByTagName("comments-count").item(0)
                        .getChildNodes().item(0).getNodeValue();
                String postedon = singlePost.getElementsByTagName("posted-on").item(0).getChildNodes().item(0)
                        .getNodeValue();
                String posttitle = "[Basecamp] " + singlePost.getElementsByTagName("title").item(0)
                        .getChildNodes().item(0).getNodeValue();
                String topicid = insertDiscussionTopic(conn, projid, posttitle, PostAuthId, postedon, PostBody,
                        jobj, lid);

                NodeList subPosts = ((Element) singlePost.getElementsByTagName("comments").item(0))
                        .getElementsByTagName("comment");
                for (int subPostCount = 0; subPostCount < subPosts.getLength(); subPostCount++) {
                    Element subPost = (Element) subPosts.item(subPostCount);
                    String subpostAuthid = subPost.getElementsByTagName("author-id").item(0).getChildNodes()
                            .item(0).getNodeValue();
                    String subpostBody = "";
                    if (subPost.getElementsByTagName("body").item(0).getChildNodes().item(0) != null) {
                        subpostBody = subPost.getElementsByTagName("body").item(0).getChildNodes().item(0)
                                .getNodeValue();
                    }
                    String subpostCreatedat = subPost.getElementsByTagName("created-at").item(0).getChildNodes()
                            .item(0).getNodeValue();
                    insertDiscussionSubPost(conn, projid, posttitle, subpostAuthid, subpostCreatedat,
                            subpostBody, jobj, lid, topicid);
                }

            }

            //                    }
        }
        conn.commit();
    } catch (Exception ex) {
        KrawlerLog.op.warn("Problem Occured while importing projects from base camp from " + ex.toString());
        DbPool.quietRollback(conn);
        result = MessageSourceProxy.getMessage("pm.project.basecamp.importerror", null, locale);
    } finally {
        DbPool.quietClose(conn);
    }
    return result;
}

From source file:org.carewebframework.shell.BaseXmlParser.java

/**
 * Parses an xml extension from an xml string.
 * //from w w  w . j a  v  a  2 s . c  o m
 * @param xml XML containing the extension.
 * @param tagName The top level tag name.
 * @return Result of the parsed extension.
 * @throws Exception Unspecified exception.
 */
protected Object fromXml(String xml, String tagName) throws Exception {
    Document document = XMLUtil.parseXMLFromString(xml);
    NodeList nodeList = document.getElementsByTagName(tagName);

    if (nodeList == null || nodeList.getLength() != 1) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "Top level tag '" + tagName + "' was not found.");
    }

    Element element = (Element) nodeList.item(0);
    Class<?> beanClass = getBeanClass(element);
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass);
    doParse(element, builder);
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.setParentBeanFactory(SpringUtil.getAppContext());
    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    factory.registerBeanDefinition(tagName, beanDefinition);
    return factory.getBean(tagName);
}

From source file:function.Functions.java

protected void printFlickrError(Document response) {
    NodeList error = response.getElementsByTagName("err");
    String code = error.item(0).getAttributes().item(0).getTextContent();
    String msg = error.item(0).getAttributes().item(1).getTextContent();
    System.out.println("Flickr request failed with error code " + code + ", " + msg);
}

From source file:com.axelor.apps.base.test.PrepareCsv.java

private String getNameColumn(String fileName) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    File domainFile = new File(fileName);
    if (!domainFile.exists())
        return null;
    Document doc = dBuilder.parse(domainFile);
    NodeList nList = doc.getElementsByTagName("entity");
    if (nList != null) {
        NodeList fields = nList.item(0).getChildNodes();
        Integer count = 0;/*from ww  w. j  av a2 s . c  o  m*/
        while (count < fields.getLength()) {
            NamedNodeMap attrs = fields.item(count).getAttributes();
            count++;
            if (attrs != null && attrs.getNamedItem("name") != null) {
                String name = attrs.getNamedItem("name").getNodeValue();
                if (name.equals("importId"))
                    return "importId";
                else if (name.equals("code"))
                    return "code";
                else if (name.equals("name"))
                    return "name";
                else
                    continue;
            }

        }
    }
    return null;
}

From source file:WSpatern.ListOfFile.java

private void parseXML(String line) {
    files.removeAllElements();//from  ww  w .j  a v  a 2 s .  c  o m
    files_Id.removeAllElements();
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("fileLinks");
        if (response.getLength() > 0) {
            for (int i = 0; i < response.getLength(); i++) {
                Element err = (Element) response.item(i);
                files.add(err.getElementsByTagName("name").item(0).getTextContent());
                files_Id.add(err.getElementsByTagName("id").item(0).getTextContent());

            }

        }
    } 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:WpRDFFunctionLibrary.java

public static Document addWpProvenance(Document currentGPML, String wpIdentifier, String wpRevision)
        throws ParserConfigurationException, SAXException, IOException {
    Element pathwayElement = (Element) currentGPML.getElementsByTagName("Pathway").item(0);
    pathwayElement.setAttribute("identifier", wpIdentifier);
    pathwayElement.setAttribute("revision", wpRevision);
    return currentGPML;
}

From source file:PayHost.Utilities.java

/**
 * Parse the resposne and get its status as object of Class "MyObject"
 *
 * @param res The response of the post http request
 * @return object of Class "MyObject"/*from   w  ww. ja va2  s.  c  o m*/
 * @throws ParserConfigurationException ParserConfigurationException
 * @throws SAXException SAXException
 * @throws IOException IOException
 */
public MyObject getStatus(String res) throws ParserConfigurationException, SAXException, IOException {
    Document doc = parseXmlFromString(res);
    NodeList nodeList = doc.getElementsByTagName("ns2:Status");
    //System.out.println(nodeList.item(0).getFirstChild().getTextContent());
    MyObject obj = new MyObject();
    //System.out.println(nodeList.item(0).getChildNodes().getLength());
    for (int i = 0; i < 5; i++) {
        obj.setProperty(nodeList.item(0).getChildNodes().item(i).getNodeName().replace("ns2:", ""),
                nodeList.item(0).getChildNodes().item(i).getTextContent());
    }
    //obj.printProperties();
    return obj;
}

From source file:WSpatern.LoginWS.java

private void parseXML(String line) {
    try {//from   ww  w.j a v a 2  s .c o m
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(line)));
        NodeList response = doc.getElementsByTagName("userToken");
        if (response.getLength() > 0) {
            Element err = (Element) response.item(0);
            expire = err.getElementsByTagName("expires").item(0).getTextContent();
            tokekn = err.getElementsByTagName("token").item(0).getTextContent();
            unitId = err.getElementsByTagName("unitId").item(0).getTextContent();
            userId = err.getElementsByTagName("userId").item(0).getTextContent();
            valid = Boolean.valueOf(err.getElementsByTagName("valid").item(0).getTextContent());
        } else {
            valid = false;
        }
    } 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:com.axelor.csv.script.PrepareCsv.java

/**
 * Get namecolumn field of entity/*from www. java2  s  . c  o  m*/
 * @param fileName
 * @return
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
private String getNameColumn(String fileName) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    File domainFile = new File(fileName);
    if (!domainFile.exists())
        return null;
    Document doc = dBuilder.parse(domainFile);
    NodeList nList = doc.getElementsByTagName("entity");
    if (nList != null) {
        NodeList fields = nList.item(0).getChildNodes();
        Integer count = 0;
        while (count < fields.getLength()) {
            NamedNodeMap attrs = fields.item(count).getAttributes();
            count++;
            if (attrs != null && attrs.getNamedItem("name") != null) {
                String name = attrs.getNamedItem("name").getNodeValue();
                if (name.equals("importId"))
                    return "importId";
                else if (name.equals("code"))
                    return "code";
                else if (name.equals("name"))
                    return "name";
                else
                    continue;
            }

        }
    }
    return null;
}

From source file:com.diversityarrays.dalclient.DalUtil.java

/**
 * <p>//  ww  w .  ja v a2s .  co m
 * Extract the &lt;Error Message="..." /&gt; from the XML response.
 * In fact it will retrieve all of the attributes from the 'Error' element
 * because some DAL responses use the attribute name as part of the message!
 * <p>
 * Note that if the Message attribute is missing, this will still produce
 * an error so it is <b>not</b> the same as just calling:
 * <code>
 * getElementAttributeValue(doc, "Error", "Message");
 * </code>
 * <br>
 * Any other attributes of the <i>Error</i> element will also be returned. This caters
 * for diagnostic errors which are returned by some of the DAL "update" operations.
 * @param doc
 * @return error message as a String or null
 */
static public String getXmlDalErrorMessage(Document doc) {
    String result = null;
    NodeList errorElements = doc.getElementsByTagName(DALClient.TAG_ERROR);
    if (errorElements.getLength() > 0) {
        Node item = errorElements.item(0);
        NamedNodeMap attributes = item.getAttributes();
        if (attributes == null) {
            result = String.format("No attributes available in '%s' element", //$NON-NLS-1$
                    DALClient.TAG_ERROR);
        } else {
            // We will get them *all*.
            StringBuilder sb = new StringBuilder();
            String sep = ""; //$NON-NLS-1$
            int nAttributes = attributes.getLength();
            for (int ai = 0; ai < nAttributes; ++ai) {
                Node attr = attributes.item(ai);
                if (attr != null) {
                    sb.append(sep).append(attr.getNodeName()).append('=').append(attr.getTextContent());
                    sep = ", "; //$NON-NLS-1$
                }
            }
            result = sb.toString();
        }
    }
    return result;
}