Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating objects from xml

 
Jamie Wool
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic