Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

creating objects from xml

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic