trimming whitespace when unmarshalling xml to POJO

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

trimming whitespace when unmarshalling xml to POJO

by dudleygb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi guys,

Whats the easiest way to trim whitespace from an incomming XML. Its unmarshalling correctly but I want to eliminate the whitespace in the POJO fields....any pointers much appreciated

Re: trimming whitespace when unmarshalling xml to POJO

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

Reply to Author | View Threaded | Show Only this Message

Hi,

dudleygb wrote:

>
> Hi guys,
>
> Whats the easiest way to trim whitespace from an incomming XML. Its
> unmarshalling correctly but I want to eliminate the whitespace in the POJO
> fields....any pointers much appreciated

Derive from the ReaderWrapper and overload the getValue method. As hack:

HierarchicalStreamDriver driver = new Xpp3DomDriver();
XStream xstream = new XStream((HierarchicalStreamDriver)null);
Object o = xstream.unmarshal(
  new ReaderWrapper(driver.createReader(inputStream))
  {
     public String getValue() {
         return super.getValue().trim();
     }
  }
);

However, if you do not want this behavior for all XML elements you might use
the getNodeName call to check the current element's name. Oh, well, and
getValue() might also return null ;-)

- Jörg


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

    http://xircles.codehaus.org/manage_email



Re: Re: trimming whitespace when unmarshalling xml to POJO

by dudleygb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cool thanx jorg, i will test for null, not a problem, will gove this a try tomorrow at the office, thanx for the response

On Wed, Oct 21, 2009 at 8:34 PM, Jörg Schaible <joerg.schaible@...> wrote:
Hi,

dudleygb wrote:

>
> Hi guys,
>
> Whats the easiest way to trim whitespace from an incomming XML. Its
> unmarshalling correctly but I want to eliminate the whitespace in the POJO
> fields....any pointers much appreciated

Derive from the ReaderWrapper and overload the getValue method. As hack:

HierarchicalStreamDriver driver = new Xpp3DomDriver();
XStream xstream = new XStream((HierarchicalStreamDriver)null);
Object o = xstream.unmarshal(
 new ReaderWrapper(driver.createReader(inputStream))
 {
    public String getValue() {
        return super.getValue().trim();
    }
 }
);

However, if you do not want this behavior for all XML elements you might use
the getNodeName call to check the current element's name. Oh, well, and
getValue() might also return null ;-)

- Jörg


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

   http://xircles.codehaus.org/manage_email