Solved

Xstream question and java object

Posted on 2010-08-22
6
324 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

17 Experts available now in Live!

Get 1:1 Help Now