Solved

Xstream question and java object

Posted on 2010-08-22
6
329 Views
Last Modified: 2012-05-10
I am looking to build java objects

<placement_rules>
  <section name="news">
    <front_order>
       <article_type name="article1"></article_type>
       <article_type name="article2"></article_type>
    </front_order>
    <back_order>
       <article_type name="article3"></article_type>
    </back_order>
  </section>
  <section name="sports">
    <front_order>
      <article_type name="article4"></article_type>
    </front_order>
    <back_order>
      <article_type name="article5"></article_type>
      <article_type name="article6"></article_type>
    </back_order>
  </section>
</placement_rules>

Open in new window


What are the objects that i need to create?

i am starting from inside SectionRule.java
@XStreamAsAttribute
@XStreamAlias("name")
private String sectionName;

@XStreamAlias("front_order")
private List<ConfigPlacementNode> frontOrder = new ArrayList<ConfigPlacementNode>(0);

@XStreamAlias("back_order")
private List<ConfigPlacementNode> backOrder = new ArrayList<ConfigPlacementNode>(0);

Open in new window


how do i create other first class objects around it? any help will be highly appreciated.

Regards, Amar
0
Comment
Question by:Amar-ms
  • 4
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33497322
It has been a while since I looked at XStream, but I don't think you want XStreamAsAttribute there since the results are in elements. You would just surround these with class that represents PlacementRules. And what you have underneath is a Section class that has in it a FrontOrder and BackOrder class that has within it a List. Again I will have to do a quick refresher on XStream if that doesn't make sense to test it out, but believe that is what you are looking for.

The tutorials are pretty decent on the site by the way:
http://xstream.codehaus.org/

Kevin
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33497327
And actually to be more precise, I would create an Order class with List then in your Section class you have two different variables: front and back. Those variables get aliases to the respective XML nodes.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33497495
Okay, I tested it and should work the way I said for you pending you tweak it for your own liking of object functionality and resulting xml but to get what you showed above I used:

@XStreamAlias("article_type")
class ArticleType {
    @XStreamAlias("name")
    @XStreamAsAttribute
    Private String name;

    // ...
}

class Order {
    @XStreamImplicit(itemFieldName="article_type")
    List articleTypes;

    // ...
}

class Section {
    // contains String (name) and two Order (front, back) members
}

class PlacementRules {
    // contains List
}

Figured that should be enough for you to give this a try. You should be able to learn more that way and with the tutorial than me giving you what I had which I threw together quick and dirty to test. You will want your code to be more appropriate to your functionality.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Amar-ms
ID: 33497818
thanks a lot i will try it and let you know.. this should be a good starting point for me.
0
 

Author Closing Comment

by:Amar-ms
ID: 33506431
Thanks a lot for your help it worked great. tjhanks for taking time out.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33506686
You are most welcome!
Best regards and happy coding,
Kevin
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question