aspose file tools*
The moose likes Java in General and the fly likes DOMParser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "DOMParser" Watch "DOMParser" New topic
Author

DOMParser

SAM KUMAR
Greenhorn

Joined: Jan 02, 2002
Posts: 25
Hi,
I am writing a helper class which provides various static method to parse XML and return Document etc. This helper class will be called by several programs.
Here the snippet:
public class MyParser {
public static Document parse(String xmlString) throws SAXException, IOException {
InputSource in = new InputSource(new StringReader(xmlString));
DOMParser parser = new DOMParser();
parser.parse(in);
return parser.getDocument();
}
//....
}
Now my question is that whether I can declare DOMParser also as a static member variable and do not instantiate every time?
public class MyParser {
private static DOMParser parser = new DOMParser();
public static Document parse(String xmlString) throws SAXException, IOException {
InputSource in = new InputSource(new StringReader(xmlString));
parser.parse(in);
return parser.getDocument();
}
//....
}
I could not understand if it will have any adverse impact?
Thanks.
Sam
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
I am not sure of what DOM API you are using, but I would suggest looking at the JavaDoc for the DOMParser class (good documentation will say whether it is thread-safe or not). I took a look at the javax.parsers.DocumentBuilder and javax.parsers.DocumentBuilderFactory classes in JDK1.4 and they are both NOT thread-safe (looked into those as a possible point of comparison).
In the case of the DocumentBuilder, you could simply synchronize the parse method so that only one thread was going through at a time... this may work for your DOM API as well.
You best bet, though would be to check the docs.


- Chris Stehno, SCPJ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOMParser