[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

mapping xml to a class or array

Hi i am wondering if it is possible to map an xml file to a java class or an array i have the following xml file and would like the Dom to be mapped to an array i have checked castor xml mapping but this site is not very clear can i map the following xml

<program>
<decs>
<decl>int rand1=0</decl>
<decl>int rand2=0</decl>
<decl>int rand3=0</decl>
</decs>
<program>

if this is possible how would this be done i am using dom
0
donnae
Asked:
donnae
  • 4
  • 4
  • 2
  • +3
1 Solution
 
CEHJCommented:
You can use commons Digester to do this
0
 
radarshCommented:
with digester, I doubt if you can write back XML from java objects... well, i'm not sure.
One thing I am sure about is Castor.

http://www.castor.org

________
radarsh
0
 
CEHJCommented:
>>with digester, I doubt if you can write back XML from java objects.

Is that required?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
0
 
donnaeAuthor Commented:
No i just want to add the text in the xml file to an array or class and output the results in an application... the class or array with int rand1=0, i want to call a method to randomly generate a character to replace the text rand1 which will then become int a=0.

0
 
objectsCommented:
xpath will do that 4u
0
 
donnaeAuthor Commented:
with xpath can you specify which elements you wish to select. im not so sure how to use xpath quite new to xml, does xpath have to be in an xsl file???
0
 
CEHJCommented:
XPath won't help you if you need xml to object marshalling. That's what digester is designed for
0
 
objectsCommented:
>  with xpath can you specify which elements you wish to select.

yes, that what it is for

> does xpath have to be in an xsl file???

no

> XPath won't help you if you need xml to object marshalling.

That is not required. Strange that you fabricate requirements after putting down an earlier suggestrion for the same thing. You've made your suggestion, please leave others to make their own.
0
 
CEHJCommented:
>>That is not required.

Seems to be to me:

>>am wondering if it is possible to map an xml file to a java class or an array
0
 
objectsCommented:
object marshalling is not required to achieve that, xpath can do that just fine
0
 
javaoptimizerCommented:
Hi , you can easily map this to a java class using CASTOR.

i.e in your class you can have String array[] to which you can map the values.

<?xml version="1.0" encoding="UTF-8"?>
<mapping>
    <class name="yourclassname" auto-complete="false">
        <description>Default mapping for class </description>
        <map-to xml="Program"/>
        <field name="rand" type="String" Collection="array"  required="true" direct="false" transient="false">
            <bind-xml name="decl" node="element" matches="Header" reference="false" location="decls/decl"/>
        </field>
    </class>
</mapping>

i think this mapping will work , but i havent tested , just check out the values of location tags and bind-xml name.
Any Doubts please do ask me
0
 
NoOodiCommented:
This Site contains an example. it will be very useful:
http://www.developerfusion.co.uk/show/2064/
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now