Link to home
Start Free TrialLog in
Avatar of donnae
donnae

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use commons Digester to do this
Avatar of radarsh
radarsh

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
>>with digester, I doubt if you can write back XML from java objects.

Is that required?
Avatar of donnae

ASKER

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.

xpath will do that 4u
Avatar of donnae

ASKER

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???
XPath won't help you if you need xml to object marshalling. That's what digester is designed for
>  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.
>>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
object marshalling is not required to achieve that, xpath can do that just fine
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
ASKER CERTIFIED SOLUTION
Avatar of NoOodi
NoOodi

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