Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating a JavaObject with the values from XML

 
Amie Alousis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to instantiate a Java Object by populating its attributes with the values from an XML
Or in other way how to convert XML to an Java Object(JavaBean)?
Thanks
Amie
 
Holger Prause
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Xalans
"Extension function Java calls"
to do this.Assuming we have the following
xml - File
<?xml version="1.0"?>
<doc>
<foo>foo</foo>
</doc>

We also have a javaclass XalanExtension for the Java - Object you would like to instantiate

public class XalanExtension {

public XalanExtension() {
System.out.println("XalanExtension instantiated from XSLT - Processor );
}
}
then we have the xsl(the stylesheet) file were we want to instantiate the XalanExtension - object
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xslt/java"
version="1.0">

<xsl:template match="/">
<xsl:apply-templates"/>
</xsl:template>

<xsl:template match="foo">

<xsl:variable name="myExt"
select="java:XalanExtension.new()"/>

</xsl:template>

</xsl:stylesheet>

when you now process the xml and xsl file with Xalan, the XalanExtension- object is created from the Xslt - processor(Xalan)

You can also call methods from this object -
for more information and a more detailed instruction see
http://xml.apache.org/xalan-j/extensions.html
Bye,
Holger
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to get to the link now, but there is an article at www.javaworld.com that talks about this. I think it is called JAT?
HTH
Brian
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick reply - use introspection to findout properties of the class and use attribute names as tags with attribute values as content.
Also store the class name so that you can create it at runtime ( XMLFile to object ).
Sorry I'm in a hurry ....
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Amie Alousis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks for helping..
Ajith, can u please explain in detail.. bec i'm using the same method.. but i cann't understand the statement,
"Also store the class name so that you can create it at runtime "
Can u please explain me..
For examples i have got the xml file like :
<?xml version="1.0" encoding='us-ascii'?>
<Customer>
<Name>
<First>Amie</First>
<Last>Alousis</Last>
</Name>
<Age>21</Age>
<Account>
<AccNum>111</AccNum>
<AccType>Checking</AccType>
</Account>
</Customer>
Where Customer, Account and Name r JavaBeans. I need to populate the values from the XML in Java Bean attributes.
I have done introspection and got the propertyDescriptor for the bean.
I have parsed the xml and got DOM object.. How to proceed from here..?
Thanks
Amie
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie,
You have to store the name of the beanclass in the XML file in order to be able to constuct it ( using newInstance() ). Perhaps you can store it as an attribute of the root element.
Eg -
<Customer BeanClassName= "CustomerBean.class">
<Name>
<First>Amie </First>
<Last>Alousis</Last>
</Name>
<Age>21</Age>
<Account>
<AccNum>111</AccNum>
<AccType>Checking</AccType>
</Account>
</Customer>
// Step 1 - get the node list from the parsed DOM tree


// Step 2 - get the name of the beanclass from the attribute and instantiate
// the class.

// Step 3 - Now iterate through the NodeList ( ie., walk the DOM tree recursively ) to // get the properties. Load each property into the BeanObject.

Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
[This message has been edited by Ajith Kallambella (edited March 21, 2001).]
 
Amie Alousis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ajith..
Amie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic