0

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://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    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.

jrhooker
  • 1,373
  • 2
  • 11
  • 14
  • 1
    cant you send just a string? – eldjon Aug 26 '14 at 05:21
  • I'm afraid "just send a string" is not sufficient noobed-down to be comprehended. Are you saying that I could store the REST output as a string, SandR it, and execute xpath queries against it? – jrhooker Aug 26 '14 at 05:40
  • yeah thats what i was wondering – eldjon Aug 26 '14 at 05:41
  • Hmmm. You know, I'm looking at my existing code and I've got all these "toString" methods and I'm wondering why I assumed that Java knew I was working with an XML file; it could be that just hitting a string object with an xpath query will work if the string is actually valid xml.....I'll try that in the morning. If it works I'll spend the rest of the morning feeling profoundly foolish. – jrhooker Aug 26 '14 at 05:55

1 Answers1

0

This question turned out to be so simple I'm considering deleting it just to prevent polluting stackoverflow; it was a basic misunderstanding of what Java could do with a String. I replaced all the file manipulation stuff with:

String fXmlFile = null;

if (fXmlFile != null) {} else {
    File collectionTree = new File(bscConnector.GetCollection());
    fXmlFile = collectionTree.toString();
    fXmlFile = fXmlFile.replace("\\", "/");
}

and other than that left my code unchanged. All works, much faster too since it's not serializing and deserializing a large text file any more.

I'm going to move the initialization of the fXmlFile out of the JSP and into the servlet, define it as a session object, and pass it in as a part of the request because right now I'm having to declare it as null right before I test to see if it's null, which seems self-defeating. Other than that, it's all good.

Thanks eldjon.

jrhooker
  • 1,373
  • 2
  • 11
  • 14