× 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.



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.