How can I convert XML config file into JavaBean?

Discussions

Web tier: servlets, JSP, Web frameworks: How can I convert XML config file into JavaBean?

  1. How can I convert XML config file into JavaBean? (4 messages)

    I have an XML file that is used as a configuration file and I need to bring this data into a JavaBean. I began working with the Jakart Commons Digester. The problem I'm having is my XML field (which can be changed) is setup as follows:

    <roles>
    <role rolename="View1">
    <field>
    <fieldname>Address</fieldname>
    <access>1</access>
    </field>
    <field>
    <fieldname>Allow Webaccess</fieldname>
    <access>0</access>
    </field>
    <field>
    <fieldname>Name</fieldname>
    <access>1</access>
    </field>
    .
    .
    .
    <field>
    <fieldname>Premium</fieldname>
    <access>0</access>
    </field>
    </role>

    <role rolename="View2">
    <field>
    <fieldname>Address</fieldname>
    <access>1</access>
    </field>
    <field>
    <fieldname>Allow Webaccess</fieldname>
    <access>0</access>
    </field>
    .
    .
    etc.

    Therefore, I cannot capture as in their book example "author" "title" "printer" etc. All fields are labled as "field" I need to dynamically call the setter methods in the Javabean. Is there a way to take say the first item "Address" and be able to call the "setAddress(int x)" method and populate it with the "Access?"

    Thanks,
    Chris
  2. Have a look at xstream http://xstream.codehaus.org/ it takes java beans and writes them to xml files
  3. Correct me if I'm missing something here, but isn't Sax extremely well suited for this? The Handler code to stream the example XML you posted is trivial, and then then code to initiate the parsing is only a few lines after that.

    Wouldn't this be suitable?
  4. I'm trying to find code that will accomplish what I want but am not having much luck. Obviously, I'm new to XML via Java. Can you provide sample code? Here's my XML file:

    <?xml version = "1.0"?>
    <!-- This is the XML that controls user access to CBN -->

    <roles>
    <role rolename="Genericview">
    <address>1</address>
    <allowwebaccess>0</allowwebaccess>
    <brokername>1</brokername>
    <brokername2>1</brokername2>
    <status>1</status>
    <brokertype>0</brokertype>
    <comments>1</comments>
    <dba>1</dba>
    <divisionofbusiness>1</divisionofbusiness>
    <email>1</email>
    <fax>1</fax>
    <marketingcoordinator>1</marketingcoordinator>
    <marketinggroup>1</marketinggroup>
    <rd>1</rd>
    <mvplevel>1</mvplevel>
    <mvpqualified>1</mvpqualified>
    <otherbrokernums>1</otherbrokernums>
    <brokernum>1</brokernum>
    <phone>1</phone>
    <phone800>1</phone800>
    <website>1</website>
    <accountopened>1</accountopened>
    <agencysystems>0</agencysystems>
    <annualrevenue>0</annualrevenue>
    <demographiccommments>0</demographiccommments>
    <eoexpdate>0</eoexpdate>
    <lastactive>1</lastactive>
    <numaccounts>0</numaccounts>
    <numpols>0</numpols>
    <numemployees>0</numemployees>
    <percentcommercial>0</percentcommercial>
    <percentlife>0</percentlife>
    <percentpersonal>0</percentpersonal>
    <specialtynichemarkets>0</specialtynichemarkets>
    <taxid>0</taxid>
    <taxidtype>0</taxidtype>
    <topdirectmarkets>0</topdirectmarkets>
    <writtenpremium>0</writtenpremium>
    </role>

    <role rolename="Anotherview">
    <address>1</address>
    <allowwebaccess>0</allowwebaccess>
             .
             .
             .
    </role>
    </roles>

    Here's my Bean:
    public class Role implements java.io.Serializable {

    private int address;
    private int allowwebaccess;
    private int brokername;
    private int brokername2;
    private int status;
    private int marketcoord;
    private int marketinggrp;
    private int rd;
    private int mvplevel;
    private int mvpqualified;
    private int brokernum;
    private int otherbrokernum;

    private int taxid;
    private int taxidtype;
    private int brokertype;
    private int acct_open_date;
    private int last_active_date;
    private int eoexpdate;
    private int is_mktgoff;
    private int numaccounts;
    private int numpols;
    private int writtenpremium;
    private int percentpersonal;
    private int percentcommercial;
    private int percentlife;
    private int email;
    private int website;
    private int numemployees;
    private int annualrevenue;
    private int phone;
    private int phone800;
    private int fax;
    private int dba;
    private int divob;

    //External Entities
    private int wintam;
    private int delphi;
    private int comments;
    private int rdname;

    public Role() {
    address = 0;
    allowwebaccess = 0;
    brokername = 0;
    brokername2 = 0;
    status = 0;
    marketcoord = 0;
    marketinggrp = 0;
    rd = 0;
    mvplevel = 0;
    mvpqualified = 0;
    brokernum = 0;
    otherbrokernum = 0;
    taxid = 0;
    taxidtype = 0;
    brokertype = 0;
    acct_open_date = 0;
    last_active_date = 0;
    eoexpdate = 0;
    is_mktgoff = 0;
    numaccounts = 0;
    numpols = 0;
    writtenpremium = 0;
    percentpersonal = 0;
    percentcommercial = 0;
    percentlife = 0;
    email = 0;
    website = 0;
    numemployees = 0;
    annualrevenue = 0;
    phone = 0;
    phone800 = 0;
    fax = 0;
    dba = 0;
    divob = 0;

    //External Entities
    wintam = 0;
    delphi = 0;
    comments = 0;
    rdname = 0;
    }


    /**
    * The {@link UserDatabase} with which we are associated.
    */

    public int getAddress() {
    return address;
    }
    public void setAddress(int x) {
    this.address = x;
    }

    public int getAllowwebaccess() {
    return allowwebaccess;
    }
    public void setAllowwebaccess(int x) {
    this.allowwebaccess = x;
    }

    public int getBrokername() {
    return brokername;
    }
    public void setBrokername(int x) {
    this.brokername = x;
    }

    public int getBrokername2() {
    return brokername2;
    }
    public void setBrokername2(int x) {
    this.brokername2 = x;
    }

    public int getStatus() {
    return status;
    }
    public void setStatus(int x) {
    this.status = x;
    }

    public int getMarketcoord() {
    return marketcoord;
    }
    public void setMarketcoord(int x) {
    this.marketcoord = x;
    }

    public int getMarketinggrp() {
    return marketinggrp;
    }
    public void setMarketinggrp(int x) {
    this.marketinggrp = x;
    }

    public int getRd() {
    return rd;
    }
    public void setRd(int x) {
    this.rd = x;
    }

    public int getMvplevel() {
    return mvplevel;
    }
    public void setMvplevel(int x) {
    this.mvplevel = x;
    }

    public int getMvpqualified() {
    return mvpqualified;
    }
    public void setMvpqualified(int x) {
    this.mvpqualified = x;
    }

    public int getBrokernum() {
    return brokernum;
    }
    public void setBrokernum(int x) {
    this.brokernum = x;
    }

    public int getOtherbrokernum() {
    return otherbrokernum;
    }
    public void setOtherbrokernum(int x) {
    this.otherbrokernum = x;
    }

    public int getTaxid() {
    return taxid;
    }
    public void setTaxid(int x) {
    this.taxid = x;
    }

    public int getTaxidtypeabbr() {
    return taxidtype;
    }
    public void setTaxidtypeabbr(int x) {
    this.taxidtype = x;
    }

    public int getBrokertype() {
    return brokertype;
    }
    public void setBrokertype(int x) {
    this.brokertype = x;
    }

    public int getAcct_open_date() {
    return acct_open_date;
    }
    public void setAcct_open_date(int x) {
    this.acct_open_date = x;
    }

    public int getLast_active_date() {
    return last_active_date;
    }
    public void setLast_active_date(int x) {
    this.last_active_date = x;
    }

    public int getEoexpdate() {
    return eoexpdate;
    }
    public void setEoexpdate(int x) {
    this.eoexpdate = x;
    }

    public int getNumaccounts() {
    return numaccounts;
    }
    public void setNumaccounts(int x) {
    this.numaccounts = x;
    }

    public int getNumpols() {
    return numpols;
    }
    public void setNumpols(int x) {
    this.numpols = x;
    }

    public int getWrittenpremium() {
    return writtenpremium;
    }
    public void setWrittenpremium(int x) {
    this.writtenpremium = x;
    }

    public int getPercentpersonal() {
    return percentpersonal;
    }
    public void setPercentpersonal(int x) {
    this.percentpersonal = x;
    }

    public int getPercentcommercial() {
    return percentcommercial;
    }
    public void setPercentcommercial(int x) {
    this.percentcommercial = x;
    }

    public int getPercentlife() {
    return percentlife;
    }
    public void setPercentlife(int x) {
    this.percentlife = x;
    }

    public int getWebsite() {
    return website;
    }
    public void setWebsite(int x) {
    this.website = x;
    }

    public int getNumemployees() {
    return numemployees;
    }
    public void setNumemployees(int x) {
    this.numemployees = x;
    }

    public int getAnnualrevenue() {
    return annualrevenue;
    }
    public void setAnnualrevenue(int x) {
    this.annualrevenue = x;
    }

    public int getPhone() {
    return phone;
    }
    public void setPhone(int x) {
    this.phone = x;
    }

    public int getPhone800() {
    return phone800;
    }
    public void setPhone800(int x) {
    this.phone800 = x;
    }

    public int getFax() {
    return fax;
    }
    public void setFax(int x) {
    this.fax = x;
    }

    public int getDba() {
    return dba;
    }
    public void setDba(int x) {
    this.dba = x;
    }

    public int getDivisionofbusiness() {
    return divob;
    }
    public void setDivisionofbusiness(int x) {
    this.divob = x;
    }



    /*
    * External Entities
    */

    public int getComments() {
    return comments;
    }
    public void setComments(int x) {
    this.comments = x;
    }
    }
  5. http://jakarta.apache.org/commons/digester/

    Alex.