You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Slava <te...@darum.ch> on 2004/05/13 09:50:36 UTC

XML Deserialization : Deserializer how to?

Hi all

I saw in archives this question appeares before (for example here
http://marc.theaimsgroup.com/?l=axis-user&m=107547213309713&w=2 )
But I could not find any answer. So I post this question again with hope to
get the hint.


Problem :

1) There is JAVA-class generated with WDSL2JAVA according to schema.
2) There is XML file wich fits to the same schema.

Task : deserialize XML to JAVA class without additional tools out of the
boundaries of AXIS-framework (Castor,JAXP etc..)


Possible solution could be like this (but this one do not work correctly):

----------------------------------------------

//For this example I use object Phone from samples/addr in standard
AXIS-distribution
//Class is generated from following schema (in
samples\addr\AddressBook.wsdl ) :
/*    <xsd:complexType name="phone">
        <xsd:all>
            <xsd:element name="areaCode" type="xsd:int"/>
            <xsd:element name="exchange" type="xsd:string"/>
            <xsd:element name="number" type="xsd:string"/>
        </xsd:all>
      </xsd:complexType>  */
//It is simple object with 3 fields


DeserializerFactory df =
     BaseDeserializerFactory.createFactory(
      Class.forName("org.apache.axis.encoding.ser.BeanDeserializerFactory"),
      Class.forName("samples.addr.Phone"),
      new QName("phone"));

org.apache.axis.encoding.Deserializer des=

(org.apache.axis.encoding.Deserializer)df.getDeserializerAs(Constants.AXIS_S
AX);

     SAXParserFactory parserFactory = SAXParserFactory.newInstance();
     parserFactory.setNamespaceAware(true);
     SAXParser parser = parserFactory.newSAXParser();
     XMLReader xmlReader = parser.getXMLReader();

      xmlReader.setContentHandler((ContentHandler)des);

//Following XML was created by org.apache.axis.encoding.ser.BeanSerializer
from JAVA-object Phone
//I expect that this XML will be correctly understood by BeanDeserializer

     String test_xml="<?xml version=\"1.0\"
encoding=\"UTF-8\"?><phone><areaCode xsi:type=\"xsd:int\" xmlns:xsi=
\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">22</areaCode><exchange
xsi:type=\"xsd:string\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">777</exchange><number
xsi:type=\"xsd:string\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">qwertyu</number></phone>";


     StringReader sr = new StringReader(test_xml);
     xmlReader.parse(new InputSource(sr));

//Problem appears here :


System.out.println("des:"+((samples.addr.Phone)des.getValue()).getNumber());

//Fields in object Phone contain no values from XML.

----------------------------------------------

What is here wrong in this example?
Why deserializer do not put values from XML into JAVA-object?
Could somebody help?


WBR Slava


AW: XML Deserialization : Deserializer how to?

Posted by Slava <te...@darum.ch>.
But how it should be implemented in context of the example below?
Could you please suggest the changes in code?

> -----Ursprüngliche Nachricht-----
> Von: John R Meloro [mailto:jmeloro@adelphia.net]
> Gesendet: Freitag, 14. Mai 2004 00:08
> An: axis-user@ws.apache.org
> Betreff: Re: XML Deserialization : Deserializer how to?
>
>
> Axis comes with Bean Serializer/Deserializer.
>
> If your bean was explicitly referenced as a return type then Axis should
> already register the Bean Serializer/Deserializer in the
> deploy.wsdd for the
> client and in the generated "stub" file for the server.
>
> If either or both of these do not show up then you will need to
> manually add
> them.
>
> ----- Original Message -----
> From: "Slava" <te...@darum.ch>
> To: <ax...@ws.apache.org>
> Sent: Thursday, May 13, 2004 3:50 AM
> Subject: XML Deserialization : Deserializer how to?
>
>
> > Hi all
> >
> > I saw in archives this question appeares before (for example here
> > http://marc.theaimsgroup.com/?l=axis-user&m=107547213309713&w=2 )
> > But I could not find any answer. So I post this question again with hope
> to
> > get the hint.
> >
> >
> > Problem :
> >
> > 1) There is JAVA-class generated with WDSL2JAVA according to schema.
> > 2) There is XML file wich fits to the same schema.
> >
> > Task : deserialize XML to JAVA class without additional tools out of the
> > boundaries of AXIS-framework (Castor,JAXP etc..)
> >
> >
> > Possible solution could be like this (but this one do not work
> correctly):
> >
> > ----------------------------------------------
> >
> > //For this example I use object Phone from samples/addr in standard
> > AXIS-distribution
> > //Class is generated from following schema (in
> > samples\addr\AddressBook.wsdl ) :
> > /*    <xsd:complexType name="phone">
> >         <xsd:all>
> >             <xsd:element name="areaCode" type="xsd:int"/>
> >             <xsd:element name="exchange" type="xsd:string"/>
> >             <xsd:element name="number" type="xsd:string"/>
> >         </xsd:all>
> >       </xsd:complexType>  */
> > //It is simple object with 3 fields
> >
> >
> > DeserializerFactory df =
> >      BaseDeserializerFactory.createFactory(
> >
> Class.forName("org.apache.axis.encoding.ser.BeanDeserializerFactory"),
> >       Class.forName("samples.addr.Phone"),
> >       new QName("phone"));
> >
> > org.apache.axis.encoding.Deserializer des=
> >
> >
> (org.apache.axis.encoding.Deserializer)df.getDeserializerAs(Consta
> nts.AXIS_S
> > AX);
> >
> >      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
> >      parserFactory.setNamespaceAware(true);
> >      SAXParser parser = parserFactory.newSAXParser();
> >      XMLReader xmlReader = parser.getXMLReader();
> >
> >       xmlReader.setContentHandler((ContentHandler)des);
> >
> > //Following XML was created by
> org.apache.axis.encoding.ser.BeanSerializer
> > from JAVA-object Phone
> > //I expect that this XML will be correctly understood by
> BeanDeserializer
> >
> >      String test_xml="<?xml version=\"1.0\"
> > encoding=\"UTF-8\"?><phone><areaCode xsi:type=\"xsd:int\" xmlns:xsi=
> > \"http://www.w3.org/2001/XMLSchema-instance\"
> > xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">22</areaCode><exchange
> > xsi:type=\"xsd:string\"
> > xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
> > xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">777</exchange><number
> > xsi:type=\"xsd:string\"
> > xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
> >
> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">qwertyu</number></phone>";
> >
> >
> >      StringReader sr = new StringReader(test_xml);
> >      xmlReader.parse(new InputSource(sr));
> >
> > //Problem appears here :
> >
> >
> >
> System.out.println("des:"+((samples.addr.Phone)des.getValue()).get
> Number());
> >
> > //Fields in object Phone contain no values from XML.
> >
> > ----------------------------------------------
> >
> > What is here wrong in this example?
> > Why deserializer do not put values from XML into JAVA-object?
> > Could somebody help?
> >
> >
> > WBR Slava
> >
>
>


Re: XML Deserialization : Deserializer how to?

Posted by John R Meloro <jm...@adelphia.net>.
Axis comes with Bean Serializer/Deserializer.

If your bean was explicitly referenced as a return type then Axis should
already register the Bean Serializer/Deserializer in the deploy.wsdd for the
client and in the generated "stub" file for the server.

If either or both of these do not show up then you will need to manually add
them.

----- Original Message ----- 
From: "Slava" <te...@darum.ch>
To: <ax...@ws.apache.org>
Sent: Thursday, May 13, 2004 3:50 AM
Subject: XML Deserialization : Deserializer how to?


> Hi all
>
> I saw in archives this question appeares before (for example here
> http://marc.theaimsgroup.com/?l=axis-user&m=107547213309713&w=2 )
> But I could not find any answer. So I post this question again with hope
to
> get the hint.
>
>
> Problem :
>
> 1) There is JAVA-class generated with WDSL2JAVA according to schema.
> 2) There is XML file wich fits to the same schema.
>
> Task : deserialize XML to JAVA class without additional tools out of the
> boundaries of AXIS-framework (Castor,JAXP etc..)
>
>
> Possible solution could be like this (but this one do not work correctly):
>
> ----------------------------------------------
>
> //For this example I use object Phone from samples/addr in standard
> AXIS-distribution
> //Class is generated from following schema (in
> samples\addr\AddressBook.wsdl ) :
> /*    <xsd:complexType name="phone">
>         <xsd:all>
>             <xsd:element name="areaCode" type="xsd:int"/>
>             <xsd:element name="exchange" type="xsd:string"/>
>             <xsd:element name="number" type="xsd:string"/>
>         </xsd:all>
>       </xsd:complexType>  */
> //It is simple object with 3 fields
>
>
> DeserializerFactory df =
>      BaseDeserializerFactory.createFactory(
>
Class.forName("org.apache.axis.encoding.ser.BeanDeserializerFactory"),
>       Class.forName("samples.addr.Phone"),
>       new QName("phone"));
>
> org.apache.axis.encoding.Deserializer des=
>
>
(org.apache.axis.encoding.Deserializer)df.getDeserializerAs(Constants.AXIS_S
> AX);
>
>      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
>      parserFactory.setNamespaceAware(true);
>      SAXParser parser = parserFactory.newSAXParser();
>      XMLReader xmlReader = parser.getXMLReader();
>
>       xmlReader.setContentHandler((ContentHandler)des);
>
> //Following XML was created by org.apache.axis.encoding.ser.BeanSerializer
> from JAVA-object Phone
> //I expect that this XML will be correctly understood by BeanDeserializer
>
>      String test_xml="<?xml version=\"1.0\"
> encoding=\"UTF-8\"?><phone><areaCode xsi:type=\"xsd:int\" xmlns:xsi=
> \"http://www.w3.org/2001/XMLSchema-instance\"
> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">22</areaCode><exchange
> xsi:type=\"xsd:string\"
> xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">777</exchange><number
> xsi:type=\"xsd:string\"
> xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">qwertyu</number></phone>";
>
>
>      StringReader sr = new StringReader(test_xml);
>      xmlReader.parse(new InputSource(sr));
>
> //Problem appears here :
>
>
>
System.out.println("des:"+((samples.addr.Phone)des.getValue()).getNumber());
>
> //Fields in object Phone contain no values from XML.
>
> ----------------------------------------------
>
> What is here wrong in this example?
> Why deserializer do not put values from XML into JAVA-object?
> Could somebody help?
>
>
> WBR Slava
>