× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Aaron,

There are several ways to do what you want, but if you have a Schema 
I would try JAXB. You feed JAXB the schema and it will generate classes

that map to your XML document. You then unmarshal the XML document 
to Java objects. You can make changes and validate those objects, which

applies your schema rules to the object context tree. When you are done

you just unmarshal the result.

JAXB is not the only way to do this. The commons digester is an event 
driven engine that makes it pretty easy to traverse XML files and
generate 
objects based on events. Other options include Castor, which is much
more 
inclusive and focuses more on data base objects, quick, and jbind. 

Since JAXB is a standard and is pretty flexible, I would consider that
first. 
I finished an article on JAXB a week or so ago and did a presentation
at 
COMMON that covers all of the options I described. If you are
interested, 
I could pass along the presentation and will post the article link when
it 
becomes available. One issue with JAXB is that it is new and you may
find 
errors with complex schemas particularly if you are overriding default

bindings. It also doesn't perform 100% of the edits that are possible
in 
a schema with unique being the most obvious omission.

Schemas are somewhat convoluted and more complex than they should 
be. The JAXB specification doesn't say you have to use .xsd schemas but

that is the only implementation at this time. I expect to see relax-ng
support 
in a follow on release, which is a simpler schema language.

David Morris 



>>> ALBartell@xxxxxxxxxxxxxx 04/10/03 08:27AM >>>
Hey all,

I have been working with XML and Java a lot lately and am wondering if
there
is a tool out there that will take an XML file or DTD or Schema and
create a
XML Java Bean. ?  This Java Bean would have getters and setters for all
of
the fields plus parse and serialize methods for XML purposes.  

Below is an example of a small ExtraCharge Java Bean that I use in an
Order
request type situation.  It would just be nice to have because of all
the
different communication that I am doing in XML.

Thanks in advance for any help you can provide,
Aaron Bartell

import org.jdom.*;
import org.jdom.input.DOMBuilder;
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.InputSource;
import java.util.List;
import java.util.LinkedList;
import java.io.CharArrayReader;

public class ExtraCharge {

    private String id = " ";
    private String shortDescription = " ";
    private Integer qty = new Integer(0);
    private Double price = new Double(0.0);

    public ExtraCharge(String x) {
        this.parse(x);
    }



    public Element serialize() {
        
            Element extraChargeRoot = new Element("ExtraCharge");
            DocType docType = new DocType("ExtraCharge"); 
            Document order = new Document(extraChargeRoot, docType);

            extraChargeRoot.addContent(new
Element("ShortDescription").addContent(shortDescription));
            extraChargeRoot.addContent(new
Element("ItemId").addContent(id));
            extraChargeRoot.addContent(new
Element("Qty").addContent(qty.toString()));
            extraChargeRoot.addContent(new
Element("BillableAmount").addContent(new
Element("Money").addContent(price.toString())));

            return extraChargeRoot;
    } 

    public void parse(String sXML) {

        // Change String to an input source so the parser.parse(is)
can
parse correctly
        char[] cArray = new char[sXML.length()];
        sXML.getChars(0, sXML.length(), cArray, 0);
        CharArrayReader car = new CharArrayReader(cArray);
        InputSource is = new InputSource(car);

        DOMBuilder builder = new DOMBuilder(true); // DOMBuilder builds
a
JDOM tree using DOM - true=validate
        DOMParser parser = new DOMParser(); // Xerces specific class

        try {
            parser.parse(is);
            org.w3c.dom.Document domDoc = parser.getDocument(); //
Create a
w3c document
            org.jdom.Document jdomDoc = builder.build(domDoc); //
Populate
the jdom Document with the w3c Document
            Element extraChargeRoot = jdomDoc.getRootElement();
            List allChildren = extraChargeRoot.getChildren();

            String name = " ";
            for (int i = 0; i < allChildren.size(); i++) {
                name = ((Element) allChildren.get(i)).getName();

                if (name.equals("ItemId")) {
                    id = ((Element) allChildren.get(i)).getText();

                } else if (name.equals("ShortDescription")) {
                    shortDescription = ((Element)
allChildren.get(i)).getText();

                } else if (name.equals("Qty")) {
                    qty = new Integer(((Element)
allChildren.get(i)).getText());

                } else if (name.equals("Money")) {
                    price = new Double(((Element)
allChildren.get(i)).getText());
                }
            }

        } catch (Exception e) {
            System.out.println("Java Error Message:" +
e.getMessage());
        }
        return;
    } // End Parse

  
  
    /**
     * Gets the id
     * @return Returns a String
     */
    public String getId() {
        return id;
    }
    /**
     * Sets the id
     * @param id The id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Gets the shortDescription
     * @return Returns a String
     */
    public String getShortDescription() {
        return shortDescription;
    }
    /**
     * Sets the shortDescription
     * @param shortDescription The shortDescription to set
     */
    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    /**
     * Gets the qty
     * @return Returns a Integer
     */
    public Integer getQty() {
        return qty;
    }
    /**
     * Sets the qty
     * @param qty The qty to set
     */
    public void setQty(Integer qty) {
        this.qty = qty;
    }

    /**
     * Gets the price
     * @return Returns a Double
     */
    public Double getPrice() {
        return price;
    }
    /**
     * Sets the price
     * @param price The price to set
     */
    public void setPrice(Double price) {
        this.price = price;
    }

}  

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.