*
The moose likes XML and Related Technologies and the fly likes Instantiating a JavaObject with the values from XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Instantiating a JavaObject with the values from XML" Watch "Instantiating a JavaObject with the values from XML" New topic
Author

Instantiating a JavaObject with the values from XML

Amie Alousis
Greenhorn

Joined: Mar 20, 2001
Posts: 4
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

Joined: Oct 09, 2000
Posts: 47
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

Joined: Nov 02, 2000
Posts: 195
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

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Amie Alousis
Greenhorn

Joined: Mar 20, 2001
Posts: 4
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

Joined: Mar 17, 2000
Posts: 5782
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

Joined: Mar 20, 2001
Posts: 4
Thanks a lot Ajith..
Amie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiating a JavaObject with the values from XML
 
Similar Threads
Java to XML
creating objects from an XML file
how to parse a xml String?
Converting Xml to Java Object
setting entity values