Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Converting an xml to a java object

HI,
   I have an xml like :

<form id="id1" channel="lobby" day="1" time="hh:mm">
    <block>Welcome to the retention Bot<block>
    <prompt after="1s">
        Hi there! this is your friendly neighbourhood flock bot! im here to show you around flock and give you cool tips on how to use flock with your team
    </prompt>
    <prompt after="1s">
         To continue, type something
    </prompt>
    <field name="email">
        <prompt after="30s">
            First things first, Lets get your team here so we can see what flock can really do, let me know an email ID you would like to invite (enter one or more emails seperated by a comma - a@directi.com, b@directi.com, c@directi.com... )
        </prompt>
        <nomatch>
             Sorry that does not look like a vaid email ID, try again.
        </nomatch>
        <noinput timeout="15s" count="1">
            Enter email IDs you'd like me to invite...
        </noinput>
        <noinput timeout="15s" count="2">
            Enter email IDs you'd like me to invite...
        </noinput>
        <noinput timeout="15s" count="3">
            Ok looks like ur busy.. we can do this later - Here's a quick How to invite (GIF)
            <next>
        </noinput>
        <submit next="email"/>
    </field>
    <prompt after="30s">
        Great! lets set up your first group - A group is like a chat room, They're great for team collaboration, updates and just fun! Create one for a project (like website, new release etc) or a team (like Deisgn or IT or HR) or any other topic.
    </prompt>
    <prompt after="5s">
        Here's a quick how to: (GIF showing group creation)
    </prompt>
    <prompt after="30s">
        If you ever need me, just open this chat and type "Help" - Lastly' here is a great intro video to Flock. (video)
    </prompt>
</form>

Open in new window


I want to convert this xml to a java object.
Please help me design a class corresponding to this xml and also what functionalities are available to convert this into a java object ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

no i dont have schema for xml
Is schema necessary for this ? is it possible to do it without schema ?
I tried following a sample xml to object conversion and it worked :
Here is the example : http://www.javatpoint.com/jaxb-unmarshalling-example

But in my case i will write several classes like for prompt, block, noinput, block.
Also after converting it to java object i want to iterate over the object elements... same way you parse xml.
Why i am doing this is.... inside each class like prompt, block, noinput...
there will be an execute method... which will run once this node is encountered.
So if i could somehow iterate over my java object i can call execute of each node (or class) that i encounter.

Is it possible to do that ?
So to simplify the question is :
say xml :
<root>
<x>1</x>
<y>2</y>
</root>

i convert it to a java object say test.

where Test can be {
int x;
int y;
}

Now one case could be suppose i want to output all elements values in the same way it is in xml.
so the output should be 1,2.
But using the test object can i do this ? Although it will have the value of x as 1 and y as 2.. But it wont know which occured first and which next... ?

Thanks
Is schema necessary for this ? is it possible to do it without schema ?

No its not necessary but makes the codegen possible.

You can do it without the schema, just handcraft some JAXB objects.


..But it wont know which occured first and which...
You can control the serialisation in JAXB using property ordering e.g. by annotating your JAXB class at class level with:
@XmlType(propOrder = {"x", "y"})
no but user can specify y element first and then x element. in that case i will have to output 2,1
the order is defined by user...
so basically i will have to traverse the xml but using the object so that i can call the appropriate function...
but if that is not posaible can you suggest an alternative approach..
To explain more what i want to achieve in simple terms
Suppose a user enters the following xml as an input to my java program  :
eg.
<form id="sd" time="10:00">
 <block> Hello </block>
  <prompt after="10s">Do you need any help</prompt>
  <filled>
     <prompt after="10s" timout="15s">Enter you email id : </prompt>
    <noinput>Enter your email id :</noinput>
   <nomatch>you entered a wrong email id</nomatch>
  </filled>
</form>

Now what this means is that...  :
At 10 am this form will become active.
The block element simply sends the message in it to the user.
The prompt element will send the message after 10s of previous message.
In the filled element we will ask user to enter an email id..
And if there is no input within 15 seconds we will timout.
And ask him once again to enter an email id.

So basically this xml is just specifying a way of communication to the user.
And it has to be executed in a top down fashion.. Although it could have <goto> tags to which can allow it
to jump from one element to another.

For handling this i though about converting it to a java object with each element having its own java class...
So that each element handling can be done appropriately and in a good design.

But the issue pops up how do i run this script.... As i will have to parse the xml somehow and execute functions.

Thanks
I think this kind of problem should have been solved .... If you can provide me some links to any open source projects... which does something like this..
Althouhg this question was mainly for parsing xml.. i will post another question regarding this separate issue.
SOLUTION
Avatar of Theo Kouwenhoven
Theo Kouwenhoven
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial