XML has embellish the organ franca of the machine industry, dynamical discover senior formats much as nymphalid distributed values and immobile earth size files for agitated accumulation between companies and applications. When compounded with newborn technologies same scheme services, it has embellish a general factor of code development.
When manipulating XML low Java, there hit been digit base approaches commonly available. The prototypal is to ingest the JDOM parser, datum the whole writing into module and then operative on it. This has the plus of requiring rattling lowercase bespoken writing to parse documents, and also allows code to create XML documents. The direct separate (apart from requiring the whole writing to sound into memory) is that the DOM created is rattling generic. Everything is spoken in cost of nodes and attributes, and it’s unmanageable to navigate.
The another move is to ingest a SAX-style event-driven parser. This is more consanguine to a tralatitious compiler, where callbacks are prefabricated whenever elements or attributes are encountered during the parse. This has the plus of existence rattling module efficient, but is modify harder to use.
A test existence is to ingest XSLT to direct alter the XML to something else, commonly an XHTML document. This is a enthusiastic move to take, forward that what you poverty to do is to create an HTML writing as an modify product.
Many times, however, every you rattling poverty to do is double the table of an XML enter into a assemblage of Plain Old Java Objects (POJOs). A artist warning of this is datum an XML plan enter on covering startup. In these types of applications, Castor is meet what the student ordered. Imagine a ultimate XML info for a bookstore:
<collection type=”private”>
<book isbn= “0345334302″>
<title>The Ringworld Engineers</title>
<author>
<lastname>Niven</lastname>
<firstname>Larry</firstname>
</author>
</book>
<book isbn= “0671462148″>
<title>Inferno</title>
<author>
<lastname>Niven</lastname>
<firstname>Larry</firstname>
</author>
<author>
<lastname>Pournelle</lastname>
<firstname>Jerry</firstname>
</author>
</book>
</collection>
There’s apparently a aggregation more that you could be storing most the aggregation (price, business date, etc.), but for this warning the accumulation shown is sufficient. One essential feature to state is that there crapper be binary authors for a azygos book.
All you poverty to do in this warning is to alluviation up every the books in the collection, and indicant discover a inform of every the authors in our collection, and the books they hit written. In visit to do this, you requirement POJOs to stop every the different levels of objects in the schema. First, the Author object, which is at the lowermost of the plan organisation (the getters and setters hit been distant for terseness…)
package com.blackbear.examples.castor;
public assemblage Author {
private String lastname;
private String firstname;
}
All that an communicator has is a lastname and firstname. The Book goal is a lowercase more complicated.
package com.blackbear.examples.castor;
import java.util.ArrayList;
import java.util.List;
public assemblage Book {
private String title;
private String isbn;
private List<Author> authors = newborn ArrayList<Author>();
}
In constituent to a denomination and ISBN number, books hit a itemize of authors, implemented as an ArrayList. Because you’re existence beatific Java 5.0 citizens, you ingest Generics to take the identify of goal that the List module hold. Finally, books go into Collection objects:
package com.blackbear.examples.castor;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;
public assemblage Collection {
private String type;
private List<Book> books = newborn ArrayList<Book>();
}
Again, you ingest Generics for the List. With our goal scheme in place, you’re primed to ingest Castor to feature in our XML file. There are digit structure to ingest Castor. One artefact is the hit the classes themselves include aggregation most how to arrange and take the XML. This is what you intend if you ingest the Eclipse Castor plug-in to create Castor POJOs from an XML XSD file. However, in this example, you’re feat to ingest the another method, which is to ingest a function file.
Technically, you crapper feature accumulation from XML into Java using Castor without process a function enter at all, but exclusive in a rattling limited ordered of circumstances. You’d requirement to delimitate your classes in the choice package, and exclusive be fascinated in insipid XML objects (in another words, elements without sub-elements.) If that were the case, datum in the accumulation would be as ultimate as saying:
Unmarshaller unmarshaller = newborn Unmarshaller();
InputStream xmlFile = Example1.class.getResourceAsStream(”books.xml”);
InputSource f = newborn InputSource(xmlFile);
try {
Object shouldBeCollection = unmarshaller.unmarshal(f);
} grownup (MarshalException e) {
e.printStackTrace();
} grownup (ValidationException e) {
e.printStackTrace();
}
First, you instantiate a double of the Castor unmarshaller. You intend an InputSource for the XML enter you poverty to parse. You call the unmarshaller on the input, and it returns an goal that module be the crowning take surroundings in the file. In this case, you’d requirement to hit a assemblage titled Collection (matching the XML surroundings study “collection”). Further, every that module be generated is a Collection goal with the identify filled out, hour of the books exclusive the assemblage module be created.
So, understandably you requirement to support Castor discover a bit. For digit thing, having to ingest the choice assemblage isn’t anything you poverty to be doing. You also poverty to wager the books in our collection, since that’s the saucer of the exercise. So you requirement to create a function enter (which you’ll call collection-mapping.xml)
<!DOCTYPE function PUBLIC “-//EXOLAB/Castor Mapping DTD Version 1.0//EN” “http://castor.org/mapping.dtd”>
<mapping>
First, a accepted DOCTYPE papers and the function element, which starts every Castor function files.
<class
name=”com.blackbear.examples.castor.Collection”>
<map-to xml=”collection”/>
<field name=”books” collection=”arraylist”
direct=”false”
type=”com.blackbear.examples.castor.Book”>
<bind-xml name=”book” node=”element”/>
</field>
<field name=”type” direct=”false”
type=”java.lang.String”>
<bind-xml name=”type” node=”attribute”/>
</field>
</class>
Recent Comments