应用程序模型是UI<->JavaServerside<->Oracle StoredProcedures[DB]
我检索从存储过程 XML-Out 接收的 XML 数据,并将其作为 JSON 对象传递到 UI。
这是片段。
import oracle.xdb.XMLType;
import org.json.JSONObject;
XMLType studentsdataXML = null;
JSONObject xmlJSONObj = null;
studentsdataXML = (XMLType) callableStatement.getObject(5);
String xmlString = studentsdataXML.getString();
xmlJSONObj = XML.toJSONObject(xmlString); // using org.json library
//return xmlJSONObj ;
上面的代码运行良好,将 XML 转换为 JSON 对象,但是性能问题是在执行 studentsdataXML.getString()
时,它大约需要总执行时间的 3/4[从 UI 回到 UI]。
问题是我是否可以直接将 XML 转换为 JSON? [oracle.xdb.XMLType 到 JSON 对象] 或对可以执行此操作的不同库的任何建议
使用的org.json库:http://www.json.org/java/
更新1:将getString()
更新为getStringVal()
即:String xmlString = StudentsdataXML.getStringVal();
getStringVal() - http://docs.oracle.com/cd/B28359_01/appdev.111/b28391/oracle/xdb/XMLType.html#getStringVal__
本文推荐使用getStringVal()获取字符串值- http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb11jav.htm#g1039140
另外,时间测量片段:
...
long stime1 = System.currentTimeMillis();
String xmlString = studentsdataXML.getStringVal();
long etime1 = System.currentTimeMillis();
log.info("Total time (in ms) for XML object to String conversion : " + (etime1 - stime1));
long stimexml = System.currentTimeMillis();
xmlJSONObj = XML.toJSONObject(xmlString);
long etimexml = System.currentTimeMillis();
log.info("Total time (in ms) for XML String to JSON conversion : " + (etimexml - stimexml));
...执行查询检索 XML 的总时间(以毫秒为单位):1308
XML 对象到字符串转换的总时间(以毫秒为单位):31452
XML 字符串到 JSON 转换的总时间(以毫秒为单位):423
Update2:另一个有类似问题的 SO 线程,但未得到答复 - Slow to convert Oracle 11g XMLType into Java String or Document
更新3:
当我在关闭连接后调用 getStringVal()
时,出现异常 - java.sql.SQLRecoverableException: Closed Connection
最佳答案
几个月前我遇到了类似的问题,经过一个多星期的搜索、测试和摸索 oracle.xdb 包,我找到了一个可行的解决方案。在我的场景中,我有一个代表大 XML 的字符串,并希望将其转换为 XMLType,以便将其保存到数据库中的 XMLTYPE 列。由于我需要辅助 oracle.sql.CLOB
作为 XMLType.createXML
方法的参数传递,因此我创建了此方法:
private CLOB createClobFromStringStreaming(String xml, Connection conn) throws SQLException, IOException {
CLOB clob = CLOB.createTemporary(conn, false, CLOB.DURATION_SESSION);
BufferedReader br = new BufferedReader(new StringReader(xml));
char[] buffer = new char[1024/*clob.getChunkSize()*/];
int read = 0;
Writer w = clob.setCharacterStream(0L);
try {
for (read = br.read(buffer); read > -1; read = br.read(buffer)) {
w.write(buffer, 0, read);
w.flush();
}
} catch (IOException e) {
throw e;
} finally {
try {
w.flush();
w.close();
br.close();
} catch (IOException e1) {
throw e1;
}
}
return clob;
}
我没有尝试将整个字符串直接放入 XMLType 变量中,而是将其分成 block 并将其流式传输到变量中。在尝试了多种不同的 block 大小之后,我发现实现最大性能的完美大小是 1024。我不知道为什么会发生这种情况,但这对我来说是最好的解决方案。
之后,我只需要调用这样的方法:
XMLType xml = XMLType.createXML(conn, this.createClobFromStringStreaming(this.eventXml, conn));
此后,我实现了正常的 XMLType 创建时间,而不是之前的 4 到 10 秒。
因此,您应该尝试类似的方法,但方向相反。尝试使用 getClobVal() 等方法从 XMLType 获取 CLOB,然后将 CLOB 转换为字符串。我不确定您是否也可以使用 getInputStream()
做一些事情,您必须尝试一下。
关于java - XML 到 JSON 转换 [oracle.xdb.XMLType 到 JSON 对象] 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26515019/