Example usage for org.jdom2 Element getChild

List of usage examples for org.jdom2 Element getChild

Introduction

In this page you can find the example usage for org.jdom2 Element getChild.

Prototype

public Element getChild(final String cname) 

Source Link

Document

This returns the first child element within this element with the given local name and belonging to no namespace.

Usage

From source file:dblp.xml.DBLPParserThirdSchema.java

private void extractElements(Element root, final String type) {
    try {//from www  .j  av  a2  s  .co m

        //            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        List<Element> nList = root.getChildren(type);
        //        System.out.println(nList);
        System.out.println(type + " " + nList.size());
        int index = 0;
        int ignore = 0;
        final Map<Integer, String> filteredConfs = readNodeFile(conf_filter_path);
        Set<String> filteredConfsSet = new HashSet<>();
        filteredConfsSet.addAll(filteredConfs.values());
        for (Element eElement : nList) {
            String booktitle = eElement.getChild("booktitle").getText();
            if (!filteredConfsSet.contains(booktitle)) {
                ignore++;
                continue;
            }
            String year = eElement.getChild("year").getText();
            int year_int = Integer.parseInt(year.trim());
            if (year_int < year_threshold) {
                ignore++;
                continue;
            }
            index++;
            if (index % 100 == 0) {
                System.out.println("Node#: " + index + " Ignore#:" + ignore);
            }
            String title = eElement.getChild("title").getText();
            int titleId = titlesCollection.get(title);
            int confId = confsCollection.get(booktitle);
            String yearConf = year + " " + booktitle;
            int yearConfId = yearConfsCollection.get(yearConf);
            title_conf_writer.write(titleId + " " + confId + "\n");
            conf_year_writer.write(confId + " " + yearConfId + "\n");
            final List<Element> authors = eElement.getChildren("author");
            for (Element author : authors) {
                String author_str = author.getText();
                int authorId = authorsCollection.get(author_str);
                title_author_writer.write(titleId + " " + authorId + "\n");
            }
        }
        title_author_writer.close();
        title_conf_writer.close();
        conf_year_writer.close();
    } catch (IOException ex) {
        Logger.getLogger(DBLPParserThirdSchema.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:DBWorkers.DBFiller.java

/**
 *    //from w  ww .j a  v  a2s.c  o  m
 * @param name ? ?
 * @param e xml-?
 * @return  xml
 */
protected String getOneField(String name, Element e) {
    if (e.getChild(name) != null) {
        String temp = e.getChild(name).getText().replaceAll("\'", "");
        if (!temp.equals("")) {
            return temp;
        }
    }
    return "?";
}

From source file:DBWorkers.DBFiller.java

/**
 *  HashMap  Xml//  w w w  . j a  v  a 2  s. c om
 * @param xml
 * @return Hash map  Xml
 */
protected HashMap<String, Object> parsePersonXML(String xml) {
    HashMap<String, Object> personData = new HashMap<String, Object>();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new InputSource(new StringReader(xml)));
        Element root = doc.getRootElement();
        Element user = root.getChild("user");

        personData.put("uid", getOneField("uid", user));
        personData.put("first_name", getOneField("first_name", user));
        personData.put("last_name", getOneField("last_name", user));
        personData.put("sex", getOneField("sex", user));
        personData.put("bdate", getOneField("bdate", user));
        personData.put("city", getOneField("city", user));
        personData.put("can_post", getOneField("can_post", user));
        personData.put("status", getOneField("status", user));
        personData.put("relation", getOneField("relation", user));
        personData.put("nickname", getOneField("nickname", user));
        personData.put("interests", getOneField("interests", user));
        personData.put("movies", getOneField("movies", user));
        personData.put("tv", getOneField("tv", user));
        personData.put("books", getOneField("books", user));
        personData.put("games", getOneField("games", user));
        personData.put("about", getOneField("about", user));
        personData.put("counters", getOneField("counters", user));
    } catch (JDOMException ex) {
        Logger.getLogger(DBCreator.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(DBCreator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return personData;
}

From source file:de.altimos.util.translator.XmlTranslation.java

License:Apache License

protected Element getElement(Element e, String[] key, int offset) {
    if (offset == key.length || e == null) {
        return e;
    }/*from   w  ww  . ja v a 2  s. c  om*/

    if (e.getChild(key[offset]) != null) {
        return getElement(e.getChild(key[offset]), key, offset + 1);
    }
    return null;
}

From source file:de.andrena.tools.macker.rule.RuleSetBuilder.java

License:Open Source License

public AccessRule buildAccessRule(final Element ruleElem, final RuleSet ruleSet) throws RulesException {
    AccessRule prevRule = null, topRule = null;
    for (final Element subElem : getChildren(ruleElem)) {
        final AccessRule accRule = new AccessRule(ruleSet);

        if (subElem.getName().equals("allow")) {
            accRule.setType(AccessRuleType.ALLOW);
        } else if (subElem.getName().equals("deny")) {
            accRule.setType(AccessRuleType.DENY);
        } else if (subElem.getName().equals("from") || subElem.getName().equals("to")
                || subElem.getName().equals("message")) {
            continue;
        } else {/*from  ww  w . ja  va  2  s .  c o m*/
            throw new RulesDocumentException(subElem, "Invalid element <" + subElem.getName() + "> --"
                    + " expected an access rule (<deny> or <allow>)");
        }

        final Element fromElem = subElem.getChild("from");
        if (fromElem != null) {
            accRule.setFrom(buildPattern(fromElem, ruleSet));
        }

        final Element toElem = subElem.getChild("to");
        if (toElem != null) {
            accRule.setTo(buildPattern(toElem, ruleSet));
        }

        if (!subElem.getChildren().isEmpty()) {
            accRule.setChild(buildAccessRule(subElem, ruleSet));
        }

        if (topRule == null) {
            topRule = accRule;
        } else {
            prevRule.setNext(accRule);
        }
        prevRule = accRule;
    }
    if (topRule != null) {
        topRule.setMessage(ruleElem.getChildText("message"));
        buildSeverity(topRule, ruleElem);
    }
    return topRule;
}

From source file:de.bund.bfr.knime.pmm.extendedtable.Model1Metadata.java

License:Open Source License

private void parseElement(Element rootElement) {

    Element agentElement = rootElement.getChild(Model1AgentXml.ELEMENT_AGENT);
    if (agentElement != null) {
        agentXml = new Model1AgentXml(agentElement);
    }/*from   w w w. j  a  v a  2  s  . c om*/

    Element matrixElement = rootElement.getChild(Model1MatrixXml.ELEMENT_MATRIX);
    if (matrixElement != null) {
        matrixXml = new Model1MatrixXml(matrixElement);
    }

    for (Element literatureElement : rootElement.getChildren(MLiteratureItem.ELEMENT_LITERATURE)) {
        modelLiteratureItems.add(new MLiteratureItem(literatureElement));
    }

    for (Element literatureElement : rootElement.getChildren(EMLiteratureItem.ELEMENT_LITERATURE)) {
        estimatedModelLiteratureItems.add(new EMLiteratureItem(literatureElement));
    }
}

From source file:de.bund.bfr.knime.pmm.extendedtable.Model2Metadata.java

License:Open Source License

private void parseElement(Element rootElement) {

    Element agentElement = rootElement.getChild(Model2AgentXml.ELEMENT_AGENT);
    if (agentElement != null) {
        agentXml = new Model2AgentXml(agentElement);
    }/*ww w  .  j ava  2  s. co  m*/

    Element matrixElement = rootElement.getChild(Model2MatrixXml.ELEMENT_MATRIX);
    if (matrixElement != null) {
        matrixXml = new Model2MatrixXml(matrixElement);
    }

    for (Element literatureElement : rootElement.getChildren(MLiteratureItem.ELEMENT_LITERATURE)) {
        modelLiteratureItems.add(new MLiteratureItem(literatureElement));
    }

    for (Element literatureElement : rootElement.getChildren(EMLiteratureItem.ELEMENT_LITERATURE)) {
        estimatedModelLiteratureItems.add(new EMLiteratureItem(literatureElement));
    }
}

From source file:de.bund.bfr.knime.pmm.extendedtable.TimeSeriesMetadata.java

License:Open Source License

private void parseElement(Element rootElement) {

    Element agentElement = rootElement.getChild(AgentXml.ELEMENT_AGENT);
    if (agentElement != null) {
        agentXml = new MDAgentXml(agentElement);
    }//from w  ww. jav  a2s.  co  m

    Element matrixElement = rootElement.getChild(MatrixXml.ELEMENT_MATRIX);
    if (matrixElement != null) {
        matrixXml = new MDMatrixXml(matrixElement);
    }

    for (Element literatureElement : rootElement.getChildren(MDLiteratureItem.ELEMENT_LITERATURE)) {
        literatureItems.add(new MDLiteratureItem(literatureElement));
    }
}

From source file:de.danielluedecke.zettelkasten.database.Bookmarks.java

License:Open Source License

/**
 * This method returns the position of a bookmarked entry-number in the bookmarks XML file.
 * if the entry-number was not bookmarked (i.e. the entry-number does not exist as bookmark-number),
 * the return value is -1//from   w w w .  j av a  2  s. co  m
 * 
 * @param nr the number of the bookmarked entry
 * @return the position of the bookmark within the xml-file, or -1 if no bookmark was found
 */
public int getBookmarkPosition(int nr) {
    // create a list of all bookmarks elements from the bookmark xml file
    try {
        // get root element
        Element bme = bookmarks.getRootElement();
        // get child element
        Element bookchild = bme.getChild("bookmark");
        // check for valid value
        if (bookchild != null) {
            // we are interested in the children of the child-element "bookmark"
            List<?> bookmarkList = bookchild.getContent();
            // an iterator for the loop below
            Iterator<?> iterator = bookmarkList.iterator();
            // counter for the return value if a found bookmark-number matches the parameter
            int cnt = 0;
            // start loop
            while (iterator.hasNext()) {
                // retrieve each element
                Element bm = (Element) iterator.next();
                // if bookmark-number matches the parameter integer value, return the position
                if (Integer.parseInt(bm.getAttributeValue("id")) == nr) {
                    return cnt;
                }
                // else increase counter
                cnt++;
            }
        }
        // if no bookmark was found, return -1
        return -1;
    } catch (IllegalStateException e) {
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
        return -1;
    }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method appends a document with zettelkasten-data to an existing
 * document.<br><br>//from w w w  .  j a  v  a 2s.  co m
 * This method is used when importing data. The imported data is appended
 * to an existing, opened data file.
 * 
 * @param zkd the zettelkasten-data in xml-document-format
 */
public void appendZknData(Document zkd) {
    // get current count, so we know at which position new entry were added
    // to the XML-document
    int currentpos = getCount(ZKNCOUNT);
    // create zettel element
    Element zettel;
    // dummy variable
    boolean mod = false;
    // remove each element. we need to use the remove-content-method, because
    // this detached the element from its former parent, so it can be added
    // to the new main-data-file
    // check whether we have any content left...
    while (zkd.getRootElement().getContentSize() > 0) {
        // try to remove/detach the child-element
        if ((zettel = (Element) zkd.getRootElement().removeContent(0)) != null) {
            // check whether the imported entry is empty or not
            if (zettel.getChild(ELEMENT_CONTENT) != null
                    && !zettel.getChild(ELEMENT_CONTENT).getText().isEmpty()) {
                try {
                    zknFile.getRootElement().addContent(zettel);
                    // set modified flag
                    mod = true;
                } catch (IllegalDataException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalAddException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                }
            }
        }
    }
    // check whether we have added any new entries
    // if so, re-convert IDs to numbers
    if (mod) {
        // go through all new added entries
        for (int cnt = currentpos + 1; cnt <= getCount(ZKNCOUNT); cnt++) {
            // retrieve each new added entry
            zettel = retrieveZettel(cnt);
            // set back reference to current last entry
            setPrevZettel(cnt, getLastZettel());
            // set pointer from current last entry to this new imported/added entry
            setNextZettel(getLastZettel(), cnt);
            // set pointer from first entry to this entry
            setPrevZettel(getFirstZettel(), cnt);
            // set next pointer from this entry to first entry
            setNextZettel(cnt, getFirstZettel());
            // set this entry as new last entry
            setLastZettel(cnt);
            //
            // here we change the entry's luhmann-numbers (trailing entries) and the
            // entry's manual links with the unique IDs
            //
            replaceAttributeIDWithNr(zettel);
            //
            // here we convert back the author IDs in footnote references
            // to the related author index numbers...
            //
            // retrieve content of entry and convert all author footnotes, which
            // contain author-index-numbers, into the related author-IDs.
            String content = zettel.getChild(ELEMENT_CONTENT).getText();
            // check for footnotes
            int pos = 0;
            while (pos != -1) {
                // find the html-tag for the footnote
                pos = content.indexOf(Constants.FORMAT_FOOTNOTE_OPEN, pos);
                // if we found something...
                if (pos != -1) {
                    // find the closing quotes
                    int end = content.indexOf("]", pos + 2);
                    // if we found that as well...
                    if (end != -1) {
                        try {
                            // extract footnote-number
                            String fn = content.substring(pos + 4, end);
                            // retrieve author ID from related footnote number
                            try {
                                int authorNr = getAuthorNumberFromID(fn);
                                // replace author number with author ID inside footnote
                                content = content.substring(0, pos + 4) + String.valueOf(authorNr)
                                        + content.substring(end);
                            } catch (NumberFormatException ex) {
                                // log error
                                Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
                                Constants.zknlogger.log(Level.WARNING,
                                        "Could not convert author ID into author number!");
                            }
                        } catch (IndexOutOfBoundsException ex) {
                        }
                        // and add it to the linked list, if it doesn't already exist
                        // set pos to new position
                        pos = end;
                    } else {
                        pos = pos + 4;
                    }
                }
            }
            // check for manual links
            pos = 0;
            while (pos != -1) {
                // find the html-tag for the manual link
                pos = content.indexOf(Constants.FORMAT_MANLINK_OPEN, pos);
                // if we found something...
                if (pos != -1) {
                    // find the closing quotes
                    int end = content.indexOf("]", pos + 2);
                    // if we found that as well...
                    if (end != -1) {
                        try {
                            // extract manual-link--number
                            String ml = content.substring(pos + 3, end);
                            // retrieve entry ID from related manual link number
                            try {
                                int zetNr = getZettelNumberFromID(ml);
                                // replace author number with author ID inside footnote
                                content = content.substring(0, pos + 3) + String.valueOf(zetNr)
                                        + content.substring(end);
                            } catch (NumberFormatException ex) {
                                // log error
                                Constants.zknlogger.log(Level.WARNING,
                                        "Could not convert entry ID into related manual link number!");
                            }
                        } catch (IndexOutOfBoundsException ex) {
                        }
                        // and add it to the linked list, if it doesn't already exist
                        // set pos to new position
                        pos = end;
                    } else {
                        pos = pos + 3;
                    }
                }
            }
            // set back changes
            zettel.getChild(ELEMENT_CONTENT).setText(content);
        }
    }
    // change modified state
    if (mod) {
        setModified(true);
    }
}