【问题标题】:XML to JSON conversion [oracle.xdb.XMLType to JSON object] performance issueXML 到 JSON 转换 [oracle.xdb.XMLType 到 JSON 对象] 性能问题
【发布时间】:2014-12-18 08:23:25
【问题描述】:

应用模型是 UIJavaServersideOracle StoredProcedures[DB]

我检索从存储过程 XML-Out 接收到的 XML 数据,并将其作为 JSON 对象传递给 UI。

这里是sn-p。

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

另外,时间测量sn-p:

...

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

【问题讨论】:

  • 是getString()方法由于从数据库中拉取或转换为字符串所用的时间吗?
  • 是转换成String的时候,因为我测量了studentsdataXML.getString();执行前后的时间
  • 我说的是内部 getString().
  • ohhk,我将 getString() 更新为 getStringVal()。但问题仍然是一样的。根据文档,该方法从 XMLType 获取包含 XML 数据的字符串值。我更新了我的帖子。当你特别问这个问题时,我注意到按照 API 抛出的异常,它说 SQLException。所以它是从 DB 中拉出来的!?
  • 也请看我的更新

标签: java xml json oracle xmltype


【解决方案1】:

几个月前我遇到了类似的问题,经过一个多星期的搜索、测试和搜索 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;
    }

我没有尝试将整个 String 直接放入 XMLType 变量,而是将其分成块并将其流式传输到变量中。在尝试了许多不同的块大小后,我发现最大性能的完美大小是 1024。我不知道为什么会发生这种情况,但这对我来说是最好的解决方案。

之后,我只需要像这样调用方法:

XMLType xml = XMLType.createXML(conn, this.createClobFromStringStreaming(this.eventXml, conn));

在这之后,我实现了 XMLType 创建的正常时间,而不是之前的 4 到 10 秒。 因此,您应该尝试类似的方法,但方向相反。尝试使用例如getClobVal() 从您的 XMLType 获取 CLOB,然后将您的 CLOB 转换为字符串。我不确定你是否也可以用getInputStream() 做点什么,你必须尝试一下。

【讨论】:

  • 虽然我还没有尝试过完整的方法,但接受了答案,但这些建议帮助了我
猜你喜欢
  • 2014-11-26
  • 1970-01-01
  • 2014-03-27
  • 2023-03-04
  • 2018-04-16
  • 2015-09-15
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
相关资源
最近更新 更多