Solved

Xstream question and java object

Posted on 2010-08-22
6
323 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Amar-ms
Comment Utility
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
Comment Utility
Thanks a lot for your help it worked great. tjhanks for taking time out.
0
 
LVL 59

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 86
firstChar challenge 13 82
Eclipse IDE - Cannot copy/paste from console output 8 58
github account with ecipse 1 15
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now