Headline

XML processing with Java's JDOM API

Characteristics

See Contribution:dom for a general motivation for exercising in-memory XML processing. The present implementation simply exercises yet another DOM-like API.

Illustration

The data model is implemented as xml files (sampleCompany.xml) that conform to a schema (Company.xsd).

Feature:Parsing is implemented by loading a Technology:JDOM Document object:

    public static Document parseCompany(String file) throws IOException, JDOMException {
        return new SAXBuilder().build(new File(file));
    }

Feature:Total and Feature:Cut are implemented using methods of Technology:JDOM's Document class:

    public static double total(Document doc) {

        // Aggregate salaries
        double total = 0;

        // Iterate over all salary elements
        Iterator<?> iterator = doc.getDescendants(new ElementFilter("salary"));
        while (iterator.hasNext()) {
            Element elem = (Element)iterator.next();
            Double salary = Double.valueOf(elem.getText());
            total += salary;
        }

        return total;
    }
    public static void cut(Document doc) {

        // Iterate over all salary elements
        Iterator<?> iterator = doc.getDescendants(new ElementFilter("salary"));

        // Snapshot these elements before modification
        List<Element> elems = new LinkedList<Element>();
        while (iterator.hasNext())
            elems.add((Element)iterator.next());

        // Iterate over salary elements and cut salaries
        for (Element elem : elems) {
            Double salary = Double.valueOf(elem.getText());
            elem.setText(Double.toString(salary/2));
        }
    }

Test cases are implemented for all Namespace:Features.

Relationships

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/java contains the following packages:
  • src/test/java contains the following packages:

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.