aspose file tools*
The moose likes Java in General and the fly likes creating objects from xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating objects from xml" Watch "creating objects from xml" New topic
Author

creating objects from xml

Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49
I have written a program with an xml parser, but am having problems when tryng to create an object from the xml data. Basically i have created a parser class to read in the xml, i then try to create an object with this data(without leaving the class). I thought this would be easier than passing the data around. Anyway i can create the object but i cannot access it from my main method(or my other methods). Can objects be created/accessed from anywhere within a program?
Greg Levine
Greenhorn

Joined: Feb 21, 2005
Posts: 21
Objects can be created anywhere, but they can only be accessed from within the scope of which they were created. If you create a local object in a function then its scope is only as long as the function is running. If you create an object as a private data member for a class then its scope is only for that class. But you can get around that by creating accessor functions in your classes that allow you to get copies of the private data members.

Ex.

Class A
{
public static void main(String args[])
{
B object = new B();
System.out.println("" + i); //doesn't work because you don't have
//access to i
}
}

Class B
{
private int i = 5;

B() //constructor
{
}
}

But,


Class A
{
public static void main(String args[])
{
B object = new B();
System.out.println("" + object.getI()); //works becuase the getI()
//function returns a copy of the
//value i
}
}

Class B
{
private int i = 5;

B() //constructor
{
}

public int getI()
{
return i;
}
}

I believe this is what you are trying to do, although my examples are in much simpler terms.
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
I'm not sure if this is what you are looking for, but I have used
the JDK 1.4.2 classes XMLEncoder and XMLDecoder to convert javabeans from xml to objects.

http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLDecoder.html

Julia
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Jamie,

Can you post some code to illustrate how the object is declared, created, and accessed? I suspect there are scoping issues for the reference variables, as Greg suggested above. However, I don't know what to suggest as a fix until I see the code.

Layne


Java API Documentation
The Java Tutorial
 
jQuery in Action, 2nd edition
 
subject: creating objects from xml