Storing an XML resource as an object rather than a file



Please turn your phasers to "noob".


As a part of my Java Servlet, I make a call to a REST resource and accept the text file returned, as below:



// check to see if the file really exists (i.e. a session is in
// progress) or we need to create one
// this should save constantly hitting the server for a new file for
// every transaction.

if (fXmlFile.exists()) {
} else {
File collectionTree = new File(bscConnector.GetCollection());
PrintWriter xmlfile = new PrintWriter(directoryName + "/outputString.xml");
xmlfile.println(collectionTree);
xmlfile.close();
}


From there I run a search and replace on it to make it valid XML file so that I can actually run xpath queries against it:



SearchAndReplace sAndR = new SearchAndReplace();

// Swap the slashes so we can actually
// query the freakin' document.
sAndR.readFiles(fXmlFile, "\\", "/");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
Document doc = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(fXmlFile);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// optional, but recommended
// read this -
// http://ift.tt/IJO9S6
doc.getDocumentElement().normalize();

// Create an instance of an xpath object
XPath xPath = XPathFactory.newInstance().newXPath();


And then I go to town on it with various xpath queries that create the interface, yadda yadda.


My question is this; while this approach works, it seems freakishly weird to be creating and querying an actual file on the server rather than doing all this in a session object, but I can't find the correct way of doing this; what object/set of objects should I be using instead of this serialize-to-disk-and-read approach?


Thanks.


No comments:

Post a Comment