• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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