• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

? How to call JAXP/JDOM from other class

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!
I created a collections class and a JDOM class by themselves from the command line and they both have main() functions which require input, but how can i call the JDCOM from the collections class when the JDOM has a main() function as well,which requires input arguments:
Here is the collecions class which basically reads a flat file and puts the fields into their appropriate field name string. For each string or field name, i then want to call the DOMEcho class and scan an XML document to see if the flat file field is within the acceptable limits, specified on the XML file. But here is the collection class and where i label the code with <<<<<<<<<<<<<<<<<<, is where i want to call the DOMEcho class:

Here is the DOMEcho class and i ran it from the command line by specifying the XML file as an argument input and if you look at this class, you will see that the class does alot of processing in the main() function before actually instantiating the DOMEecho class itself, so my question is how can i call the DOMEcho class from the collection class? It seems like i'd have to call the DOMEcho.main(args) function before instantiating the DOMEcho class, but how can i call the main() funtion without first instantiating the DOMEcho class!!! Here is the DOMEcho class and notice that in the main() function, it instantiates the DOMEcho class, see <<<<<<<<<<<<<<<<<<<<<<<<<<,:

Thanks alot for spending your valuable time on this!
Bob

EDIT: Added UBB CODE tags to help readability (increases your chances of getting an answer by a magnitude )
[ March 25, 2004: Message edited by: Lasse Koskela ]
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you get this to compile...?
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lasse!
I was in the middle of trying different things, ex. that import statement, and i cut and pasted that in there on the fly, sorry about that!
But since posting this, i'm now wondering if i should instantiate the DOM class first and then call the collection class from the DOM class as it looks like it may be easier to instantiate the collection class?
But still, it's very strange having 2 main() functions to worry about and i'm not sure i understand how to get around making sure i satisfy both of the arguments for each!

Thanks alot for try to help out Lasse!
Bob
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having difficulty understanding what you want to accomplish with these two programs?
So far, all I can say is that if a class has a main() method, it's usually intended to be used by calling that main() method instead of instantiating the class and then calling some instance methods.
I.e.
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok, so i don't actually have to use a contructor before referencing any of the methods then, very interesting?
Basically, what i'm trying to do, is to read a flat file, 'test file', which contains records with each record holding 2 comma delimited fields and for each of these fields, i want to read the XML file and find a match on that XML file by field name, ie instrument. Once i've found the field name on the XML file, i then want to make sure that the data on the 'test file' conforms to the acceptable list or range of values which are specified by field XML file.
Once the JAVA program has found the appropriate <field> <name> on the .xml file, the program will then need to compare the test file (field - value), ex (instrument - 'stock') with the .xml (field - value/s) to see if the test (file - value) is on the .xml LIST of acceptable values.
And this would be same for the 'amount' field, but the 'amount' field on the .xml file is different in that it represents a RANGE of values, instead of a
LIST of values, ex from (1 to 100000)!

------------------------------------------------
TEST RECORD (PIPE DELIMITED):
instrument|amount|instrument|amount|instrument|amount
or
stock|10000|bond|5000|cash|2000|............
or
instrument amount
---------- --------
stock 10000
bond 5000
cash 2000
option 100
debenture 50000
swapation 500
put 100
call 300
convertable 5555
-----------------------------------------------
XML RECORD:
<validation>
<field>
<name> instrument </name>
<list> stock </list>
<list> bond </list>
<list> cash </list>
<list> option </list>
<list> debeenture </list>
<list> ...........</list>
</name>
</field>
<field>
<name> amount </name>
<range> 1 </range>
<range> 100000 </range>
</name>
</field>
</valadation>
Lasse, does this sound like a logical way to use an XML file?
And thanks again for you help!
Bob
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lasse!
I found this code on a previous post and this code is the kind of simple code i was hoping to find, but when i downloaded the xerces samples and sun sample they were all pretty complex! but this i can relate too:
--------------------------------------------------------------------------------
public class BasicDOM(){ Public BasicDOM(string xmlfile) { DOMParser parser = new DOMParser(); try { parser.parse("D:\My Documents\xmlfile.xml");//my xml file location Document document = parser.getDocument(); traverse(document); } catch (SAXException e) { System.err.println (e); } catch (IOException e) { System.err.println(e); } } private void traverse(Node node) { int type = node.getNodeType(); if (type == Node.ELEMENT_NODE) System.out.println(node.getNodeName()); NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) traverse(children.item(i)); } }
Thanks!
Bob
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oh ok, so i don't actually have to use a contructor before referencing any of the methods then, very interesting?

Java has two kinds of methods: static methods and instance methods. The static methods (a.k.a. class methods) can be invoked without having a reference to an instance of the particular class. The instance methods can only be invoked on an instance of the particular class.
If the static method "saySomething()" is defined in a class named "MyClass", you can call it with MyClass.saySomething("foo").
If the instance method "saySomething()" is defined in a class named "MyClass", you can call it with new MyClass().saySomething("foo").

Lasse, does this sound like a logical way to use an XML file?


You're basically specifying validation rules in an XML file, which makes sense -- it's one of those things that XML is well suited for.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing... I would approach the XML document's structure differently:
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the ideas on the XML layout Lasse!
I will definitely follow your pointers there!
Also Lasse, i'm wondering about that import statement, and wanted to say that the DOM class is in the same directory as the collection class, so would i need an import statement in the collection class being the fact that the DOM class is in the same directory where i'm doing the collection JAVAC command?
And another thing, i'm really concerned about how efficient the XML look up is going to be versus a regular database lookup because it looks like it's going to do a simple scan to get to the correct field node, or will the DOM XML structure allow me to create keys of some sort, so i can go directly to the correct field node, does that make sense?
Thanks again Lasse!
Bob
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i'm wondering about that import statement, and wanted to say that the DOM class is in the same directory as the collection class, so would i need an import statement in the collection class being the fact that the DOM class is in the same directory where i'm doing the collection JAVAC command?

No, class "foo.bar.Xyz" does not need to import class "foo.bar.Abc" because they are in the same package.

And another thing, i'm really concerned about how efficient the XML look up is going to be versus a regular database lookup because it looks like it's going to do a simple scan to get to the correct field node, or will the DOM XML structure allow me to create keys of some sort, so i can go directly to the correct field node, does that make sense?

I suspect that the "validation.xml" file will be relatively small so you might want to read the XML document into a DOM tree once and then make the lookups against that in-memory object model. That should be a lot faster than getting a JDBC connection somewhere and executing SQL statements...
An alternative (for reading the XML into a DOM tree and querying that) would be to construct a custom object model from the validation file, something which is more easily used by the "collection" class of yours. Something that provides an interface for calling myValidationConfig.getRules("amount") and so on.
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lasse!
Thanks again for the help!
What i tried to do is call that DOM class from the collection class
I get some errors saying that i need to handle some exceptions:
C:\jwsdp-1.3\jaxp\samples\DOMEcho>javac rflat1.java
.\DOMEcho3.java:354: unreported exception javax.xml.parsers.ParserConfigurationE
xception; must be caught or declared to be thrown
DocumentBuilder db = dbf.newDocumentBuilder();
^
.\DOMEcho3.java:358: unreported exception java.io.UnsupportedEncodingException;
must be caught or declared to be thrown
new OutputStreamWriter(System.err, outputEncoding);
^
.\DOMEcho3.java:363: unreported exception org.xml.sax.SAXException; must be caug
ht or declared to be thrown
Document doc = db.parse(new File(filename));
^
.\DOMEcho3.java:369: unreported exception java.io.UnsupportedEncodingException;
must be caught or declared to be thrown
new OutputStreamWriter(System.out, outputEncoding);
^
4 errors
Is there a generic exception class that i can use to call all exceptions as i'm not going to know what the specific excpetion classes are for those
Thanks again Lasse and only when you have time!
Here is the collector class trying to call the DOM class:
import java.io.*;
import java.util.*;
public class rflat1
{
class rflat1
.............
ublic static void main(String[] args)
{
if (args.length != 1)
System.err.println("Usage: java FileCopy " +
"<source file> <destination file>");
else
{
try
{
DOMEcho3 DOM = new DOMEcho3();
try
{
DOM.DOMcall(); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
File infil = new File(sfile);
InputStream src = new FileInputStream(infil);
List recordsr = new ArrayList();
recordsr = parse(src);
System.out.println("output " + recordsr);
}
catch (IOException e) { System.err.println(e.getMessage()); }
}
}
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bob connolly:
Is there a generic exception class that i can use to call all exceptions as i'm not going to know what the specific excpetion classes are for those

java.lang.Exception?
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help Lasse!
I think i've got alot of homework to do this weekend on this problem!
Have a great weekend and good to know your out there!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic