xmlmapper:
java mapping of xml documents
SourceForge.net Logo

Description download howto

package xmlmapper

package xmlmapper

Description
Mapping example
Advantages and drawbacks
Remarks
Links
How to use this package?
xmlmapper javadoc

Description

This package creates a simple mapping of a given xml document to java, using the apache Xerces or the Electric library. The code design allows to easily implement new parser libraries.
This is an open source project funded by Fabien Le Floc'h in 2000.
 

Mapping example

<Document>
<Title>blabla</Title>
<Item id="1">
</Item>
<Item id="2">
</Item>
</Document>
import java.util.*;
public class Document {
    private String text;
    private String cDataSection;
    private ArrayList titleList;
    private ArrayList itemList;
    public Document() {
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getText() {
        return text;
    }
    public void setCDataSection(String cDataSection) {
        this.cDataSection = cDataSection;
    }
    public String getCDataSection() {
        return cDataSection;
    }
    public void setTitleList(ArrayList titleList) {
        this.titleList = titleList;
    }
    public ArrayList getTitleList() {
        return titleList;
    }
    public void setItemList(ArrayList itemList) {
        this.itemList = itemList;
    }
    public ArrayList getItemList() {
        return itemList;
    }
}


Two other objects are generated on the same principle: Title.java and Item.java.

Multiple Item objects will be stored in the itemList property of the Document object. The only restriction in this mapping is the text and CDATA fields. Only one text field an one CDATA field is taken in account in order to render the usability easier without having a big lack of functionnality.  It is in deed very rare to have to use several text fields in the same XML element.

This package is also able to initialize (dynamically) theses generated classes with the values contained in the xml file, and to write back xml after you assigned new values to the java objects.

The mapping is not based on DTD nor on XML schemes. Therefore, if you add one new type of element, you need to rebuild the classes. If you know already you will use some other XML elements, you then have to declare them in the XML. This mapping choice was done in order to have a simple mapping package, and also in order not to be limited only to DTD or only to XML schemes. This particular choice makes you more productive.
 

Advantages and drawbacks

This mapping allows regular java programmers not to worry about the xml manipulation, they just use the generated objects. So they don't have to parse the tree nor to know DOM or SAX.
Furthermore, as the parsing is only done preliminarly (at the source code generation, and at the variables initialisation), the usage of such a mapping will hopefully improve the performance of your application.
The code design allows also any programmer to add new classes and make vailable new parsers transparently to the user.

With a very big XML file, as it will be entirely mapped into memory, the resulting application won't be as efficient as if it would use SAX. A future solution could be to have an EJB mapping. But then it would become a little bit less easy to use.
 

Remarks

The code was inspired from the enhydra project.
 

Links

How to use this package ?
The enhydra project
The apache xerces project