• 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

DOMParser

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic