Hi,
 
I came across some problems while converting XML to Java Objects.
Here I am listing my problems, I request you to post me the answers for my problems in the replies
 
Consider, I am having some 'dat.xml' file which has some data.
Also, I am having some existing java classes.
I want to instantiate the java objects based on the data present in the 'dat.xml'
The following are my example codes and the problem is with the output.
Some details are not extracted from the XML and not stored in the java objects.
I wanted to know why the problem is occurring and how can i solve the problem
 

1. dat.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<single>
<trace begin="PT" id="traceId_0" timeRef="timeRefId_0" >116 24 113 25 110 27 106 29 100 32 95 35 90 37 87 40 85 42 84 43 83 44 83 45 82 45 80 46 78 47 76 48 75 49 73 49 72 50 70 51 69 52 67 53 64 54 63 54 62 55 60 56 56 58 52 59 48 60 45 61 43 61 41 62 38 63 35 65 31 66 26 67 22 68 17 69 13 69 9 70 5 71 0 72 -3 73 -5 73 -7 73 -9 73 -11 73 -14 73 -17 73 -21 73 -22 73 -23 73 -23 72 -23 69 -23 66</trace>
<timestamp id="timeRefId_0" value="Wed Mar 30 14:41:32 IST 2005"/>
</single>
 

2. Trace.java
 
/*
 * Created on May 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package castor9;
 
/**
 * @author Venkatesh Babu T.S.
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Trace {
    String tbegin = new String();
    String tid    = new String();
    String timeref= new String();
    /**
     * @return Returns the tbegin.
     */
    public String getTbegin() {
        return tbegin;
    }
    /**
     * @param tbegin The tbegin to set.
     */
    public void setTbegin(String tbegin) {
        this.tbegin = tbegin;
    }
    /**
     * @return Returns the tid.
     */
    public String getTid() {
        return tid;
    }
    /**
     * @param tid The tid to set.
     */
    public void setTid(String tid) {
        this.tid = tid;
    }
    /**
     * @return Returns the timeref.
     */
    public String getTimeref() {
        return timeref;
    }
    /**
     * @param timeref The timeref to set.
     */
    public void setTimeref(String timeref) {
        this.timeref = timeref;
    }
   
}
 
 
 
3. Time.java
 
/*
 * Created on May 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package castor9;
 
/**
 * @author Venkatesh Babu T.S.
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Time {
    String tsid = new String();
    String tsval= new String();
    /**
     * @return Returns the tsid.
     */
    public String getTsid() {
        return tsid;
    }
    /**
     * @param tsid The tsid to set.
     */
    public void setTsid(String tsid) {
        this.tsid = tsid;
    }
    /**
     * @return Returns the tsval.
     */
    public String getTsval() {
        return tsval;
    }
    /**
     * @param tsval The tsval to set.
     */
    public void setTsval(String tsval) {
        this.tsval = tsval;
    }
}
 

4. Single.java
 
/*
 * Created on May 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package castor9;
 
/**
 * @author Venkatesh Babu T.S.
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Single {
   
    String tval   = new String();
    Time ts = new Time();
    Trace t = new Trace();
    /**
     * @return Returns the t.
     */
    public Trace getTrace() {
        return t;
    }
    /**
     * @param t The t to set.
     */
    public void setTrace(Trace t) {
        this.t = t;
    }
    /**
     * @return Returns the ts.
     */
    public Time getTs() {
        return ts;
    }
    /**
     * @param ts The ts to set.
     */
    public void setTs(Time ts) {
        this.ts = ts;
    }
    /**
     * @return Returns the t.
     */
    public Time getTime() {
        return ts;
    }
    /**
     * @param t The t to set.
     */
    public void setTime(Time t) {
        this.ts = t;
    }
 
    /**
     * @return Returns the tval.
     */
    public String getTval() {
        return tval;
    }
    /**
     * @param tval The tval to set.
     */
    public void setTval(String tval) {
        this.tval = tval;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        // TODO Auto-generated method stub
        StringBuffer sb = new StringBuffer();
        sb.append("tval     : " + tval + "\n");
        sb.append("tbegin   : " + t.getTbegin() + "\n");
        sb.append("tid      : " + t.getTid()+ "\n");
        sb.append("timeref  : " + t.getTimeref() + "\n");       
        sb.append("tsid     : " + ts.getTsid() + "\n");
        sb.append("tsval    : " + ts.getTsval() + "\n");    
        return sb.toString();
    }
}
 
 
 
5. Test.java
 
/*
 * Created on May 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package castor9;
 
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
 
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
 
/**
 * @author Venkatesh Babu T.S.
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Test {
 
    public static void main(String[] args) {
   
    try {
           
        Mapping map = new Mapping();
        map.loadMapping("castor9/map.xml");
        File file = new File("castor9/dat.xml");      
      
        Reader reader = new FileReader(file);
        Unmarshaller unmarshaller = new Unmarshaller(Single.class);
        unmarshaller.setMapping(map);
       
        Single single = (Single) unmarshaller.unmarshal(reader);       
      
        System.out.println("The output is : \n" + single.toString());
       
    } catch (MappingException e) {
        e.printStackTrace(System.err);
    } catch (Exception e) {
        e.printStackTrace();
    }                        
    }
}
 

6. map.xml
 
<!DOCTYPE databases PUBLIC
  "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
  "http://castor.exolab.org/mapping.dtd">
<mapping>
  <class name="castor9.Trace">
   <map-to xml="trace"/>
    <field name="tbegin" type="java.lang.String">
     <bind-xml name="begin" node="attribute"/>
    </field> 
    <field name="tid" type="java.lang.String">
     <bind-xml name="id" node="attribute"/>
    </field>
    <field name="timeref" type="java.lang.String">
     <bind-xml name="timeRef" node="attribute"/>
    </field>   
  </class>
  <class name="castor9.Time">
    <field name="tsid" type="java.lang.String">
     <bind-xml name="id" node="attribute"/>
    </field>
    <field name="tsval" type="java.lang.String">
     <bind-xml name="value" node="attribute"/>
    </field> 
  </class>
  <class name="castor9.Single">    
    <map-to xml="trace"/>   
    <field name="tval" type="java.lang.String">
     <bind-xml name="trace" node="element"/>
    </field>       
    <field name="trace" type="castor9.Trace">
     <bind-xml name="trace"/>
    </field>
    <field name="time" type="castor9.Time">
     <bind-xml name="timestamp"/>
    </field>     
  </class>
</mapping>
 
 
7. Finally My output of the this program is
 
The output is :
tval     : 116 24 113 25 110 27 106 29 100 32 95 35 90 37 87 40 85 42 84 43 83 44 83 45 82 45 80 46 78 47 76 48 75 49 73 49 72 50 70 51 69 52 67 53 64 54 63 54 62 55 60 56 56 58 52 59 48 60 45 61 43 61 41 62 38 63 35 65 31 66 26 67 22 68 17 69 13 69 9 70 5 71 0 72 -3 73 -5 73 -7 73 -9 73 -11 73 -14 73 -17 73 -21 73 -22 73 -23 73 -23 72 -23 69 -23 66
tbegin   :
tid      :
timeref  :
tsid     : timeRefId_0
tsval    : Wed Mar 30 14:41:32 IST 2005
 

in this i am missing some details from the xml values.
 
Please help me to solve this problem. It's important one for my module.
 
 
 
 

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Reply via email to