Wonderflub
asked on
How to pass a returned value to a variable
I am a brand spanking new Java programmer. I am using a SAX parser to Parse an XML file. I need to make the element searched for a dynamic variable with the value pulled from another XML file.
public void startElement(String namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{
String eName = lName; // Set element name equal to local name
if ("".equals(eName)) eName = qName;
if (eName.equals("w:t")) <<<<<<< this value needs to be pulled from another XML file
emit("<"+eName);
nl();
I would like to use a new seperate class to do this in the interest of reusable code. How do I call that outside class from the above code and pass the returned value. Also I am simply reusing the already created parser with a few modifications to do this, however it is written to pass the output to the Standard output,
out = new OutputStreamWriter(System. out, "UTF8");
What do I need to change in order for this to pass teh variable simply back to the code.
THanks for all your help in advancce.
public void startElement(String namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{
String eName = lName; // Set element name equal to local name
if ("".equals(eName)) eName = qName;
if (eName.equals("w:t")) <<<<<<< this value needs to be pulled from another XML file
emit("<"+eName);
nl();
I would like to use a new seperate class to do this in the interest of reusable code. How do I call that outside class from the above code and pass the returned value. Also I am simply reusing the already created parser with a few modifications to do this, however it is written to pass the output to the Standard output,
out = new OutputStreamWriter(System.
What do I need to change in order for this to pass teh variable simply back to the code.
THanks for all your help in advancce.
ASKER
The information, or variable value, needs to be passed in from an external class file as they want me to create a reusable class that can be used in different applications, not jsut for this one. so I need to parse the information I need from one class file, and send it to the other class file where indicated.
I know this is all very complicated for a newbie, but this is what they asked me to figure out, so I am trying to figure it out. Before two weeks ago I only knew VB.
I know this is all very complicated for a newbie, but this is what they asked me to figure out, so I am trying to figure it out. Before two weeks ago I only knew VB.
pass the class to your ctor and store a reference to it
public MyClass
{
private MyOtherClass xxx;
public MyClass(MyOtherClass xxx)
{
this.xxx = xxx;
}
...
Then when you need to call the other class
String value = xxx.getValue();
public MyClass
{
private MyOtherClass xxx;
public MyClass(MyOtherClass xxx)
{
this.xxx = xxx;
}
...
Then when you need to call the other class
String value = xxx.getValue();
ASKER
I was out of town due to the death of my father and just returned monday. I have only begun to try the variations of solutions offered and would appreciate this question being left open for jsut a bit longer. THank you.
ASKER
I have been trying to get objects solution to work but perhaps I am not familiar enough with Java to make it work. It is not calling the external class properly. It just ain't workin.
>>I would like to use a new seperate class to do this in the interest of reusable code.
It's reusability doesn't depend on the number of entities you use, but on the way you design the code. You don't actually need to use another class to make it reusable. For maxiumum reusability:
1. Make the class read a text file, then changing search critieria is a matter of changing that text file, not recompiling code
2. If you use an ordinary text file, you can configure a List of elements to emit, giving you 0..n flexibility as opposed to 0..1
It's reusability doesn't depend on the number of entities you use, but on the way you design the code. You don't actually need to use another class to make it reusable. For maxiumum reusability:
1. Make the class read a text file, then changing search critieria is a matter of changing that text file, not recompiling code
2. If you use an ordinary text file, you can configure a List of elements to emit, giving you 0..n flexibility as opposed to 0..1
ASKER
They want the dynamic information stored in an XML file, not a text file. So the dynamic XML file needs to be parsed with a seperate class that is not coded into the original proggy. Don't ask me to understand corporate thinking, I just do what I am told. Hell, they are asking a VB programmer to build Rome in a day with java, does that even make sense. But I never say die, I just try to use the resources available to me, like genius programmers around the world :-).
> It is not calling the external class properly. It just ain't workin.
Spoecifically what are the problems?
Spoecifically what are the problems?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
ASKER
Hey sorry that took so long, I took on about 4 projects before I could come back to this, plus life happened too, go figure but thanks you gave me jsut what I needed :-)
No problem ;-)
Properties props = new Properties();
props.load(new FileInputStream("your-sear
String searchElement = props.getProperty("searchE
....
if (eName.equals(searchElemen
// do it
}