Convert Java objects to Strings

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

Convert Java objects to Strings

by Keith Thomas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am looking for a tidy way to persist my Java objects in HBase without resorting to serializing objects. Simple seems to have great possibilities for me. The HBase api I am using is here if you are interested,
http://hadoop.apache.org/hbase/docs/current/api/index.html
As you can see I need to add my data as byte arrays. To achieve this I am trying to do the following,

1. Use Simple to convert my Java classes to XML Strings,
    e.g. assert myAddress.equals("<address><street>7 Hillcrest Rd</street><city>Bristol</city></address>")
2. Convert the Strings containing XML to byte arrays
3. Put the byte arrays in my HBase database

Steps 2 & 3 are easy for me but I am stuck at Step 1, how to create a String containing an XML respresentation of my object. Any suggestions helping to achieve this would be greatly appreciated.

Many thanks.

Re: Convert Java objects to Strings

by Quirino Zagarese-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
if I got what you want to do, I guess you have to override the equals method.

//in class Address
public boolean equals(Object o) {
        if (!(o instanceof String)) {
            return false;
        }else{
            String xml = (String) o;
            String thisXml = null;
            Serializer serializer = new Persister();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try {
                serializer.write(this, stream);
            } catch (Exception e) {
                e.printStackTrace();
            }
            thisXml = stream.toString();
            return thisXml.equals(xml);
        }
    }

Obviously you have to use Simple annotations on classes' fields or getters as described in the tutorial.
Hope this helps!


2009/11/7 Keith Thomas <keith.thomas@...>

I am looking for a tidy way to persist my Java objects in HBase without
resorting to serializing objects. Simple seems to have great possibilities
for me. The HBase api I am using is here if you are interested,
http://hadoop.apache.org/hbase/docs/current/api/index.html
As you can see I need to add my data as byte arrays. To achieve this I am
trying to do the following,

1. Use Simple to convert my Java classes to XML Strings,
   e.g. assert myAddress.equals("<address><street>7 Hillcrest
Rd</street><city>Bristol</city></address>")
2. Convert the Strings containing XML to byte arrays
3. Put the byte arrays in my HBase database

Steps 2 & 3 are easy for me but I am stuck at Step 1, how to create a String
containing an XML respresentation of my object. Any suggestions helping to
achieve this would be greatly appreciated.

Many thanks.
--
View this message in context: http://old.nabble.com/Convert-Java-objects-to-Strings-tp26235457p26235457.html
Sent from the Simple XML Serialization mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support



--
Quirino Zagarese

Italian OpenLaszlo Community  - www.laszloitalia.org

EU4RIA: Laszlo+Java, easily - eu4ria.googlecode.com

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support

Re: Convert Java objects to Strings

by Keith Thomas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry that I confused with the 'equals' but despite my hokey description you have given me the answer on how to convert my Java objects to XML in String form. Many thanks.

Quirino Zagarese-2 wrote:
Hi,
if I got what you want to do, I guess you have to override the equals
method.

//in class Address
public boolean equals(Object o) {
        if (!(o instanceof String)) {
            return false;
        }else{
            String xml = (String) o;
            String thisXml = null;
            Serializer serializer = new Persister();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try {
                serializer.write(this, stream);
            } catch (Exception e) {
                e.printStackTrace();
            }
            thisXml = stream.toString();
            return thisXml.equals(xml);
        }
    }

Obviously you have to use Simple annotations on classes' fields or getters
as described in the
tutorial<http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php>
.
Hope this helps!


2009/11/7 Keith Thomas <keith.thomas@gmail.com>

>
> I am looking for a tidy way to persist my Java objects in HBase without
> resorting to serializing objects. Simple seems to have great possibilities
> for me. The HBase api I am using is here if you are interested,
> http://hadoop.apache.org/hbase/docs/current/api/index.html
> As you can see I need to add my data as byte arrays. To achieve this I am
> trying to do the following,
>
> 1. Use Simple to convert my Java classes to XML Strings,
>    e.g. assert myAddress.equals("<address><street>7 Hillcrest
> Rd</street><city>Bristol</city></address>")
> 2. Convert the Strings containing XML to byte arrays
> 3. Put the byte arrays in my HBase database
>
> Steps 2 & 3 are easy for me but I am stuck at Step 1, how to create a
> String
> containing an XML respresentation of my object. Any suggestions helping to
> achieve this would be greatly appreciated.
>
> Many thanks.
> --
> View this message in context:
> http://old.nabble.com/Convert-Java-objects-to-Strings-tp26235457p26235457.html
> Sent from the Simple XML Serialization mailing list archive at Nabble.com.
>
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Simple-support mailing list
> Simple-support@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/simple-support
>



--
Quirino Zagarese

Italian OpenLaszlo Community  - www.laszloitalia.org

EU4RIA: Laszlo+Java, easily - eu4ria.googlecode.com

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Simple-support mailing list
Simple-support@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/simple-support