Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DOMParser

 
SAM KUMAR
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic