【发布时间】:2014-08-26 05:15:48
【问题描述】:
请把你的移相器调成“菜鸟”。
作为我的 Java Servlet 的一部分,我调用 REST 资源并接受返回的文本文件,如下所示:
// 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();
}
从那里我运行搜索并替换它以使其成为有效的 XML 文件,以便我可以实际运行 xpath 查询:
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();
然后我带着各种创建界面的 xpath 查询去镇上,yadda yadda。
我的问题是这样的;虽然这种方法有效,但在服务器上创建和查询实际文件而不是在会话对象中执行所有这些操作似乎非常奇怪,但我找不到正确的方法;我应该使用什么对象/对象集来代替这种序列化到磁盘并读取的方法?
谢谢。
【问题讨论】:
-
你不能只发送一个字符串吗?
-
恐怕“只发送一个字符串”还不足以让人理解。你是说我可以将 REST 输出存储为字符串、SandR 并针对它执行 xpath 查询?
-
是的,这就是我想知道的
-
嗯。你知道,我正在查看我现有的代码,我有所有这些“toString”方法,我想知道为什么我认为 Java 知道我正在使用 XML 文件;如果字符串实际上是有效的 xml,那么只需使用 xpath 查询命中字符串对象就可以工作.....我会在早上尝试。如果它有效,我会在早上剩下的时间里感到非常愚蠢。