Headline

Object-XML mapping with JAXB of the Java platform

Characteristics

Language:XML import and export is supported for a Java-based implementation by means of O/X mapping. The primary data model for companies is an XML schema. The schema compiler Technology:xjc of Technology:JAXB is used to generate Java classes from the schema. In this manner, operations on an XML representation of companies can be implemented in near-to-regular OO fashion while using a problem-specific object model. In different terms, one can carry out XML processing while essentially staying in the technological space of objectware. It is insightful to compare XML schema and schema-derived classes. The XML schema is defined in a manner that the resulting object model systematically leverages object composition and no class inheritance. In fact, the schema-derived classes are very similar to a regular OO design; see Contribution:javaComposition. It is important to note that the operations on companies are not implemented as instance methods since this would imply modification of schema-derived classes---unless advanced modularization mechanisms were leveraged. Instead, the operations are implemented as static methods in non-schema-derived classes.

Illustration

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

 <xs:element name="department">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="name"/>
    <xs:element name="manager" type="employee"/>
    <xs:element ref="department" maxOccurs="unbounded" minOccurs="0"/>
    <xs:element name="employee"
        type="employee" maxOccurs="unbounded" minOccurs="0"/>
   </xs:sequence>
  </xs:complextype>
 </xs:element>    

Actual Language:Java classes will be generated using Technology:xjc.

Feature:Open serialization is implemented using Technology:JAXB Un-/Marshaller:

    public static Company deserializeCompany(File input)
    throws JAXBException 
    {
        initializeJaxbContext();
        Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
        return (Company) unMarshaller.unmarshal(input);
    }

    public static void serializeCompany(File output, Company c)
    throws     JAXBException,
            FileNotFoundException,
            XMLStreamException 
    {
        initializeJaxbContext();
        OutputStream os = new FileOutputStream(output);
        Marshaller marshaller = jaxbContext.createMarshaller();
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os);
        marshaller.marshal(c, writer); // TODO: need a stream writer that does indentation
    }

Feature:Total and Feature:Cut are implemented as static methods:

public class Total {

	public static double total(Company c) {
		double total = 0;
		for (Department d : c.getDepartment())
			total += total(d);
		return total;
	}

	public static double total(Department d) {
		double total = total(d.getManager());
		for (Department s : d.getDepartment())
			total += total(s);
		for (Employee e : d.getEmployee())
			total += total(e);
		return total;
	}

	public static double total(Employee e) {
		return e.getSalary();
	}

}
public class Cut {

	public static void cut(Company c) {
		for (Department d : c.getDepartment())
			cut(d);
	}

	public static void cut(Department d) {
		cut(d.getManager());
		for (Department s : d.getDepartment())
			cut(s);
		for (Employee e : d.getEmployee())
			cut(e);
	}

	public static void cut(Employee e) {
		e.setSalary(e.getSalary() / 2);
	}

}

Test cases are implemented for all Namespace:Features.

Relationships

For DOM-like implementations of in-memory XML processing see Contribution:dom, Contribution:jdom and Contribution:xom.

For a query-based implementation of in-memory XML processing see Contribution:xpathAPI.

For push-based XML processing see Contribution:sax.

For Object/XML mapping see Contribution:jaxbChoice (XSD with choice for different subunits), Contribution:jaxbComposition (XSD with object composition), Contribution:jaxbExtension (XSD with type extension) and Contribution:jaxbSubstitution (XSD with substitution groups).

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.