Returns of values to "null" from XML.

View: New views
2 Messages — Rating Filter:   Alert me  

Returns of values to "null" from XML.

by jrenteri :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have XML file.

<tranBlock>
<tran ID="71915" companyID="CNRANCOMM" auxID="1453486657">
<T.2.06.0>
<eventTS>2009-04-05T17:46:03Z</eventTS>
<equipment ID="TRA857062" equipType="tractor" unitAddress="0000857062" mobileType="1"/>
<position lon="-77.766388889" lat="-10.741666667" posTS="2009-04-05T17:46:03Z"/>
<posType>3</posType>
<ignitionStatus>2</ignitionStatus>
<tripStatus>O</tripStatus>
<ltdDistance>0.0</ltdDistance>
</T.2.06.0>
</tran>
<tran ID="71916" companyID="CNRANCOMM" auxID="1453486657">
<T.2.06.0>
<eventTS>2009-04-05T17:49:55Z</eventTS>
<equipment ID="GROM838663" equipType="tractor" unitAddress="0000838663" mobileType="1"/>
<position lon="-74.946666667" lat="-13.362222222" posTS="2009-04-05T17:49:55Z"/>
<posType>3</posType>
<ignitionStatus>2</ignitionStatus>
<tripStatus>O</tripStatus>
<ltdDistance>0.0</ltdDistance>
</T.2.06.0>
</tran>
.
.
.
.
<tranBlock>

public class TranBlockBean {
       
        private List<TranBean> tran = new ArrayList<TranBean>();
         
        public TranBlockBean() {
        }
   
        public void add(TranBean TranBean) {
        tran.add(TranBean);
        }
        public List<TranBean> getTran() {
        return tran;
        }
       
}

public class TranBean {
       
        private String     ID;
        private String     companyID;
        private String     auxID;
        private T2060Bean  t2060;
        private T4010Bean  t4010;
       
        public TranBean() {
        }
       
        public void setID(String ID){
                this.ID = ID;
        }
        public String getID(){
                return this.ID;
        }
       
        public void setCompanyID (String companyID){
                this.companyID = companyID;
        }
        public String getCompanyID(){
                return this.companyID;
        }
       
        public void setAuxID(String auxID){
                this.auxID = auxID;
        }
        public String getAuxID(){
                return this.auxID;
        }
       
        public void setT2060(T2060Bean t2060){
                this.t2060 = t2060;
        }
        public T2060Bean getT2060(){
                return this.t2060;
        }
       
        public void setT4010(T4010Bean t4010){
                this.t4010 = t4010;
        }
        public T4010Bean getT4010(){
                return this.t4010;
        }
       
}


public class TestLectura {
       
        public static void main(String[] args) {
               
                TranBlockBean tranblock = new TranBlockBean();
               
                try {
                       
                        System.out.println("Comienza el test escritura de XStream ...");
                       
                        //Creamos una instancia de Xtream
                XStream xstream = new XStream(new DomDriver());
                xstream.alias("tranBlock",TranBlockBean.class);
                xstream.alias("tran",TranBean.class);
                xstream.aliasField("T.2.06.0", TranBean.class, "t2060");
                xstream.aliasField("T.4.01.0", TranBean.class, "t4010");
                xstream.addImplicitCollection(TranBlockBean.class, "tran", TranBean.class);
                xstream.alias("T.2.06.0",T2060Bean.class);
                xstream.alias("T.4.01.0",T4010Bean.class);
                xstream.alias("equipment",EquipmentBean.class);
                xstream.alias("position",PositionBean.class);
               
                FileInputStream xml = new FileInputStream("D:/Qualcomm/data/transacciones.xml");
                xstream.fromXML(xml, tranblock);

               
                System.out.println("Nº de Entradas: " + tranblock.getTran().size());
                for(TranBean tran:tranblock.getTran()){
                                System.out.println("ID        " + tran.getID());
                                System.out.println("CompanyID " + tran.getCompanyID());
                                System.out.println("AuxID     " + tran.getAuxID());
                }

                System.out.println("Ejecutado test correctamente");
                       
                } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("Tal vez debas ejecutar primero TestEscritura.java");
                }
               
        }

}

I obtain the following result:

Comienza el test escritura de XStream ...
Nº de Entradas: 80
ID        null
CompanyID null
AuxID     null
ID        null
CompanyID null
AuxID     null
ID        null
CompanyID null
AuxID     null

Why return value null.

Any idea?

Thanks.

Re: Re[xstream-user] turns of values to "null" from XML.

by Jörg Schaible-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

jrenteri wrote:

>
> Hi,
>
> I have XML file.
>
> <tranBlock>
> <tran ID="71915" companyID="CNRANCOMM" auxID="1453486657">
> <T.2.06.0>
> <eventTS>2009-04-05T17:46:03Z</eventTS>
> <equipment ID="TRA857062" equipType="tractor" unitAddress="0000857062"
> mobileType="1"/>
> <position lon="-77.766388889" lat="-10.741666667"
> posTS="2009-04-05T17:46:03Z"/>
> <posType>3</posType>
> <ignitionStatus>2</ignitionStatus>
> <tripStatus>O</tripStatus>
> <ltdDistance>0.0</ltdDistance>
> </T.2.06.0>
> </tran>
> <tran ID="71916" companyID="CNRANCOMM" auxID="1453486657">
> <T.2.06.0>
> <eventTS>2009-04-05T17:49:55Z</eventTS>
> <equipment ID="GROM838663" equipType="tractor" unitAddress="0000838663"
> mobileType="1"/>
> <position lon="-74.946666667" lat="-13.362222222"
> posTS="2009-04-05T17:49:55Z"/>
> <posType>3</posType>
> <ignitionStatus>2</ignitionStatus>
> <tripStatus>O</tripStatus>
> <ltdDistance>0.0</ltdDistance>
> </T.2.06.0>
> </tran>
> .
> .
> .
> .
> <tranBlock>
>
> public class TranBlockBean {
>
> private List<TranBean> tran = new ArrayList<TranBean>();
>
> public TranBlockBean() {
> }
>    
> public void add(TranBean TranBean) {
>         tran.add(TranBean);
> }
> public List<TranBean> getTran() {
>         return tran;
> }
>
> }
>
> public class TranBean {
>
> private String     ID;
> private String     companyID;
> private String     auxID;
> private T2060Bean  t2060;
> private T4010Bean  t4010;
>
> public TranBean() {
> }
>
> public void setID(String ID){
> this.ID = ID;
> }
> public String getID(){
> return this.ID;
> }
>
> public void setCompanyID (String companyID){
> this.companyID = companyID;
> }
> public String getCompanyID(){
> return this.companyID;
> }
>
> public void setAuxID(String auxID){
> this.auxID = auxID;
> }
> public String getAuxID(){
> return this.auxID;
> }
>
> public void setT2060(T2060Bean t2060){
> this.t2060 = t2060;
> }
> public T2060Bean getT2060(){
> return this.t2060;
> }
>
> public void setT4010(T4010Bean t4010){
> this.t4010 = t4010;
> }
> public T4010Bean getT4010(){
> return this.t4010;
> }
>
> }
>
>
> public class TestLectura {
>
> public static void main(String[] args) {
>
> TranBlockBean tranblock = new TranBlockBean();
>
> try {
>
> System.out.println("Comienza el test escritura de XStream ...");
>
> //Creamos una instancia de Xtream
> XStream xstream = new XStream(new DomDriver());
> xstream.alias("tranBlock",TranBlockBean.class);
> xstream.alias("tran",TranBean.class);
> xstream.aliasField("T.2.06.0", TranBean.class, "t2060");
> xstream.aliasField("T.4.01.0", TranBean.class, "t4010");
> xstream.addImplicitCollection(TranBlockBean.class, "tran",
> TranBean.class);
> xstream.alias("T.2.06.0",T2060Bean.class);
> xstream.alias("T.4.01.0",T4010Bean.class);
> xstream.alias("equipment",EquipmentBean.class);
> xstream.alias("position",PositionBean.class);
>
> FileInputStream xml = new
> FileInputStream("D:/Qualcomm/data/transacciones.xml");
> xstream.fromXML(xml, tranblock);
>
>
> System.out.println("Nº de Entradas: " +
> tranblock.getTran().size());
> for(TranBean tran:tranblock.getTran()){
> System.out.println("ID        " + tran.getID());
> System.out.println("CompanyID " + tran.getCompanyID());
> System.out.println("AuxID     " + tran.getAuxID());
> }
>
> System.out.println("Ejecutado test correctamente");
>
> } catch (Exception e) {
> e.printStackTrace();
> System.out.println("Tal vez debas ejecutar primero TestEscritura.java");
> }
>
> }
>
> }
>
> I obtain the following result:
>
> Comienza el test escritura de XStream ...
> Nº de Entradas: 80
> ID        null
> CompanyID null
> AuxID     null
> ID        null
> CompanyID null
> AuxID     null
> ID        null
> CompanyID null
> AuxID     null
>
> Why return value null.
>
> Any idea?

The best way to configure XStream for an existing XML is always to write the
object and look if the XML matches the format of the one you want to read.
You would have recognized then immediately that your ID fields are not
written as attributes and therefore XStream will not look for those in the
attributes at deserialization time. You have to configure this.

- Jörg

>
> Thanks.



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email