How to represent nodes that have same element name, but different structure

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

How to represent nodes that have same element name, but different structure

by Andy Barlow [Deftex] :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
I've looked through the Tutorials and found no obvious answer to this.

I use an SMS provider which has a RESTful API for sending text and voice SMS messages.

The XML posted differs as follows, depending on the type of SMS you want to send:


An example voice send would look something like:

<?xml version='1.0' encoding='UTF-8'?>
<message>
<to>0123456789</to>
<type>Voice</type>
<body>
<lang>en-GB</lang>
<text>This is a Voice message</text>
</body>
</message>

An example SMS send would look something like:

<?xml version='1.0' encoding='UTF-8'?>
<message>
<to>0123456789</to>
<type>SMS</type>
<body>
This is a SMS message
</body>
</message>

In the Voice SMS XML, the <body> element contains two child elements: in the text XML, it just contains text.

How could this be represented in a single Java class, given the <body> element differs depending on the type?

Whilst I could see how you could put this into two classes and get the serialised result (XML) you wanted, how would you know how to deserialise it out of the XML without knowing in advance which type of message you were dealing with?

I'm new to this, so please forgive me if there is an obvious answer in the online documentation!


Regards,
Andy Barlow


The information in this email or facsimile is confidential and is intended solely for the addressee(s) and access to this email or facsimile by anyone else is unauthorised. If you are not the intended recipient then any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. Information expressed in this email or facsimile is not given or endorsed by my firm or employer unless otherwise indicated by an authorised representative independent of this message.

Deftex LimitedComputer Consultancy
 
Andy Barlow MBCS CENG EURING CITPDeftex Limited
ConsultantHigh Riding
Richmond
North Yorkshire
DL10 5BN
Skype Meª!
skype: andybarlow285
 
andy.barlow@...tel: +44 (0)208 123 6285
http://www.deftex.co.ukmobile: +44 (0)7830 302 268
 


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support

Re: How to represent nodes that have same element name, but different structure

by Niall Gallagher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
 
I think you would need two classes to do this with Simple currently. Free text in the body can be done with the @Text annotation, however it can not be combined with the @Element annotation. You could do it with some hacking in the Strategy, but it would not be very nice.
 
Niall

--- On Sat, 10/17/09, Andy Barlow [Deftex] <andy.barlow@...> wrote:

From: Andy Barlow [Deftex] <andy.barlow@...>
Subject: [Simple-support] How to represent nodes that have same element name, but different structure
To: simple-support@...
Date: Saturday, October 17, 2009, 3:20 AM

I've looked through the Tutorials and found no obvious answer to this.

I use an SMS provider which has a RESTful API for sending text and voice SMS messages.

The XML posted differs as follows, depending on the type of SMS you want to send:


An example voice send would look something like:

<?xml version='1.0' encoding='UTF-8'?>
<message>
<to>0123456789</to>
<type>Voice</type>
<body>
<lang>en-GB</lang>
<text>This is a Voice message</text>
</body>
</message>

An example SMS send would look something like:

<?xml version='1.0' encoding='UTF-8'?>
<message>
<to>0123456789</to>
<type>SMS</type>
<body>
This is a SMS message
</body>
</message>

In the Voice SMS XML, the <body> element contains two child elements: in the text XML, it just contains text.

How could this be represented in a single Java class, given the <body> element differs depending on the type?

Whilst I could see how you could put this into two classes and get the serialised result (XML) you wanted, how would you know how to deserialise it out of the XML without knowing in advance which type of message you were dealing with?

I'm new to this, so please forgive me if there is an obvious answer in the online documentation!


Regards,
Andy Barlow


The information in this email or facsimile is confidential and is intended solely for the addressee(s) and access to this email or facsimile by anyone else is unauthorised. If you are not the intended recipient then any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. Information expressed in this email or facsimile is not given or endorsed by my firm or employer unless otherwise indicated by an authorised representative independent of this message.

Deftex Limited Computer Consultancy
 
Andy Barlow MBCS CENG EURING CITP Deftex Limited
Consultant High Riding
Richmond
North Yorkshire
DL10 5BN
Skype Meª!
skype: andybarlow285
 
andy.barlow@... tel: +44 (0)208 123 6285
http://www.deftex.co.uk mobile: +44 (0)7830 302 268
 


-----Inline Attachment Follows-----

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference

-----Inline Attachment Follows-----

_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support