You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Krishna Gopal Binyala <kg...@gmail.com> on 2009/06/18 14:45:34 UTC

removing nil tags from response

Hello,

I am using XMLBeans 2.2.0 for data binding in my web service; i.e. request
and response.

If for some of the values in my response are null/ nil.. the response tag is
coming as

<Data xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

i don't want these nil value. the "Data" element is defined as
  <xsd:element name="Data" type="xsd:string" minOccurs="0"/>

please let me know how can i get rid fo these
Any help is appreciated.


Thanks
Krishna

Re: removing nil tags from response

Posted by Krishna Gopal Binyala <kg...@gmail.com>.
Hello  Jeff,

Thanks for reply..... The code works fine with small correction
.........
        cursor.removeXml() ;
        cursor.toPrevToken(); // to be added
............
// if the toPrevToken() is not used, it will not remove two consecutive nil
elements.
// because removeXml() will set the cursor to the next available token and
that next token is skipped due to condition  in while loop.

Regards,
Krishna

On Thu, Jun 18, 2009 at 7:06 PM, Jeff Lusted <jl...@star.le.ac.uk> wrote:

> Hi Krishna!
>
> On Thu, 2009-06-18 at 18:15 +0530, Krishna Gopal Binyala wrote:
> > I am using XMLBeans 2.2.0 for data binding in my web service; i.e.
> > request and response.
> >
> > If for some of the values in my response are null/ nil.. the response
> > tag is coming as
> >
> > <Data xsi:nil="true"
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
> >
> > i don't want these nil value. the "Data" element is defined as
> >   <xsd:element name="Data" type="xsd:string" minOccurs="0"/>
> >
> > please let me know how can i get rid fo these
>
> Something like this should work:
>
> XmlCursor cursor = mydoc().newCursor() ;
> try {
>   do {
>      if( cursor.isStart() && cursor.getObject().isNil() ) {
>         cursor.removeXml() ;

            cursor.toPrevToken();
// if we are not using toPrevToken(). then this code will not remove two
continuous nil elements

>
>      }
>   } while( !cursor.toNextToken().isNone() ) ;
> }
> finally {
>   cursor.dispose() ;
> }
>
> As cursor.removeXml() will remove the element, you may need to
> experiment with the loop condition to make it full-proof.
>
> Regards
> Jeff
> --
> Jeff Lusted               tel: +44 (0)116 252 3581
> Astrogrid Project         mob: +44 (0)7941 599062
> Dept Physics & Astronomy  email: jl99@star.le.ac.uk
> University of Leicester   web: http://www.astrogrid.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>

Re: removing nil tags from response

Posted by Jeff Lusted <jl...@star.le.ac.uk>.
Hi Krishna!

On Thu, 2009-06-18 at 18:15 +0530, Krishna Gopal Binyala wrote:
> I am using XMLBeans 2.2.0 for data binding in my web service; i.e.
> request and response.
> 
> If for some of the values in my response are null/ nil.. the response
> tag is coming as 
> 
> <Data xsi:nil="true"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
> 
> i don't want these nil value. the "Data" element is defined as 
>   <xsd:element name="Data" type="xsd:string" minOccurs="0"/>
> 
> please let me know how can i get rid fo these

Something like this should work:

XmlCursor cursor = mydoc().newCursor() ;
try {
   do {	
      if( cursor.isStart() && cursor.getObject().isNil() ) {
         cursor.removeXml() ;
      }
   } while( !cursor.toNextToken().isNone() ) ;
}
finally {
   cursor.dispose() ;
}

As cursor.removeXml() will remove the element, you may need to
experiment with the loop condition to make it full-proof.

Regards
Jeff
-- 
Jeff Lusted               tel: +44 (0)116 252 3581
Astrogrid Project         mob: +44 (0)7941 599062
Dept Physics & Astronomy  email: jl99@star.le.ac.uk
University of Leicester   web: http://www.astrogrid.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org