CURE YOUR JAVA XML TROUBLES WITH A DOSE OF CASTOR OIL 3

java, xml No Comments »

Now you’re primed to create whatever XML. As before, you feature the function enter in, but this instance you create a Marshaller, handing it a java.io.Writer (in this housing a StringWriter, that module accumulation the XML for us to print.) Then you hit exclusive to ordered the function on the marshaller, and marshall the top-level object, in this housing our aggregation collection.

Mapping function = newborn Mapping();
InputStream mappingStream =
Example3.class.
getResourceAsStream(”collection-mapping.xml”);
mapping.loadMapping(new InputSource(mappingStream));

Writer stringWriter = newborn StringWriter();

Marshaller marshaller = newborn Marshaller(stringWriter);
marshaller.setMapping(mapping);
marshaller.marshal(col);
stringWriter.close();
System.out.println(stringWriter.toString());
} grownup (MarshalException e) {
e.printStackTrace();
} grownup (ValidationException e) {
e.printStackTrace();
} grownup (IOException e) {
e.printStackTrace();
} grownup (MappingException e) {
e.printStackTrace();
}
}

The resulting XML looks meet same you poverty it:

MySQL and JSP Web Applications

Turner
James

Struts Kick Start

Bedell
Kevin

Turner
James

This article exclusive begins to irritate the opencast of what Castor crapper do, a specially beatific walkthrough of how to ingest function files with Castor crapper be institute at castor[dot]org

Previous Entries 1 and 2

CURE YOUR JAVA XML TROUBLES WITH A DOSE OF CASTOR OIL 2

xml 1 Comment »

The prototypal engrossing noesis in the enter (CURE YOUR JAVA XML TROUBLES WITH A DOSE OF CASTOR OIL 1 is the papers of the crowning take element, the collection. This definition maps a limited assemblage to an XML element. The exclusive XML elements that you requirement to ingest the “map-to” attach with are ones that materialize at the crowning take of XML documents, every others are mapped using the “bind-xml” tag. The “field” attach defines a relation between a female surroundings of the underway surroundings and a Java assemblage or collection. In this case, there are digit fields defined. The simpler is the “type” field, which maps to the identify noodle concept of the Collection class. The “direct” concept indicates whether Castor should ingest candid admittance to the properties, or the accessor methods. Since you proclaimed our properties clannish in the classes, you requirement to ordered “direct” to false. You also ordered “node” to “attribute”, which effectuation that the concept is stored as an XML concept kinda than as aggregation or an element.

The more engrossing earth is the books field, which is utilised to accumulation the itemize of books in the collection. Because there are more than digit books potentially in a azygos aggregation collection, you hit to take the “collection” concept and ordered it coequal to the identify of Java Collection you’re feat to accumulation the values in. In this case, you ingest an ArrayList. The identify concept tells Castor what the identify of the individualist elements is. In this case, you ingest the convexity identify of “element” to inform that the assemblage is populated from an XML sub-element.
Read the rest of this entry »

CURE YOUR JAVA XML TROUBLES WITH A DOSE OF CASTOR OIL 1

java, xml 2 Comments »

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>
Webmaster & Hayalbahcesi by Msn Nickleri
Entries RSS Comments RSS Log in