?
Solved

How to pass a returned value to a variable

Posted on 2005-03-14
14
Medium Priority
?
215 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:Wonderflub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13539146
Why not use a Properties file to hold your search value?

Properties props = new Properties();
props.load(new FileInputStream("your-search.properties"));
String searchElement = props.getProperty("searchElement1");

....

if (eName.equals(searchElement)) {
    // do it
}
0
 

Author Comment

by:Wonderflub
ID: 13547131
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13549638
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();
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Wonderflub
ID: 13727275
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.
0
 

Author Comment

by:Wonderflub
ID: 13761238
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13761352
>>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
0
 

Author Comment

by:Wonderflub
ID: 13761590
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 :-).
0
 
LVL 92

Expert Comment

by:objects
ID: 13767855
> It is not calling the external class properly.  It just ain't workin.

Spoecifically what are the problems?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13771647
If they want you to use a separate xml file, you can just wrap a List in a custom class and then use this (example in docs):

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLEncoder.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14072001
:-)
0
 

Author Comment

by:Wonderflub
ID: 14072095
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 :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14072108
No problem ;-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question