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

xml Add comments

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.

The rest of the enter follows the aforementioned pattern. You delimitate a book, with an ISBN cipher and title. The ISBN is an attribute, and the denomination comes from the table of the denomination element. The aggregation also has a itemize of authors, meet as the assemblage had a itemize of books. Notice that you don’t modify hit to delimitate the communicator class, Castor module amount discover how to fill the digit fields because the Java properties correct the XML surroundings names.

	<class name=”com.blackbear.examples.castor.Book”>
		<field name=”isbn” direct=”false”
						   type=”java.lang.String”>
			<bind-xml name=”isbn” node=”attribute”/>
	   </field>
		<field name=”title” direct=”false”
						   type=”java.lang.String”>
			<bind-xml name=”title” node=”element”/>
	   </field>
		<field name=”authors” collection=”arraylist”
						   direct=”false”
		   type=”com.blackbear.examples.castor.Author”>
			<bind-xml name=”author” node=”element”/>
	   </field>
	</class>
</mapping>

Now it’s meet a concern of weight the function file, doing an unmarshall using the mapping, and travel the resulting Java objects. As you crapper wager from the cipher below, you exclusive requirement most 8 lines of Java to actually take the XML into POJOs, the rest of the cipher is processing the resulting objects to creation a itemize of books by apiece author.

 open noise vacuum main(String[] args) {
    essay {
        Mapping function = newborn Mapping();
        InputStream mappingStream =
            Example2.class.getResourceAsStream(”collection-mapping.xml”);
        mapping.loadMapping(new InputSource(mappingStream));

        Unmarshaller unmarshaller = newborn Unmarshaller();
        InputStream xmlFile =
            Example2.class.getResourceAsStream(”books.xml”);
        InputSource f = newborn InputSource(xmlFile);
        unmarshaller.setMapping(mapping);
        Object shouldBeCollection = unmarshaller.unmarshal(f);

	  // Done demarshalling XML, rest is processing

        if (shouldBeCollection instanceof Collection) {
            Collection assemblage = (Collection) shouldBeCollection;
            Map<String, List> authors = newborn HashMap<String, List>();
            if (collection.getBooks() != null) {
                List<Book> books = collection.getBooks();
                int numBooks = books.size();
                for (int i = 0; i < numBooks; i++) {
                    Book aggregation = books.get(i);
                    List<Author> bookAuthors = book.getAuthors();
                    int numAuthors = bookAuthors.size();
                    for (int j = 0; j < numAuthors; j++) {
                        Author communicator = bookAuthors.get(j);
                        String study = author.getLastname()
                                + “,”
                                    + author.getFirstname();
                        List authorBooks = authors.get(name);
                        if (authorBooks == null) {
                            authorBooks = newborn ArrayList<Book>();
                            authors.put(name, authorBooks);
                        }
                        authorBooks.add(book);
                    }
                }
                Iterator<String> keys = authors.keySet().iterator();
                patch (keys.hasNext()) {
                    String key = keys.next();
                    System.out.println(key);
                    List<Book> authorBooks = authors.get(key);
                    int numAuthorBooks = authorBooks.size();
                    for (int i = 0; i < numAuthorBooks; i++) {
                        Book aggregation = authorBooks.get(i);
                        System.out.println(”   ”
                                + book.getTitle() + “(”
                                + book.getIsbn() + “)”);
                    }
                    System.out.println(”");
                }
            }
        }
    } grownup (MarshalException e) {
        e.printStackTrace();
    } grownup (ValidationException e) {
        e.printStackTrace();
    } grownup (MappingException e) {
        e.printStackTrace();
    }
}

Running this cipher results in the production below:

Niven,Larry
   The Ringworld Engineers(0345334302)
   Inferno(0671462148)

Pournelle,Jerry
   Inferno(0671462148)

You crapper also invoke things around and marshall up whatever XML with Castor from the aforementioned Java objects. Let’s essay the cipher exhibit here. First you create a unify of unpaid Java books to invoke into XML:

public noise vacuum main(String[] args) {
    essay {
        Collection notch = newborn Collection();
        Book aggregation = newborn Book();
        book.setTitle(”MySQL and JSP Web Applications”);
        book.setIsbn(”0672323095″);
        Author james = newborn Author();
        james.setFirstname(”James”);
        james.setLastname(”Turner”);
        book.getAuthors().add(james);
        col.getBooks().add(book);
        aggregation = newborn Book();
        book.setTitle(”Struts Kick Start”);
        book.setIsbn(”0672324725″);
        Author kevin = newborn Author();
        kevin.setFirstname(”Kevin”);
        kevin.setLastname(”Bedell”);
        book.getAuthors().add(kevin);
        book.getAuthors().add(james);
        col.getBooks().add(book);

One Response to “CURE YOUR JAVA XML TROUBLES WITH A DOSE OF CASTOR OIL 2”

  1. CURE YOUR JAVA XML TROUBLES WITH A DOSE OF CASTOR OIL 3 | Twoostwo Says:

    [...] Previous Entries 1 and 2 [...]

Leave a Reply

Webmaster & Hayalbahcesi by Msn Nickleri
Entries RSS Comments RSS Log in