简体   繁体   English

如何读取用 Node.js 查询的 Oracle 数据库 XML 列?

[英]How to read Oracle Database XML column queried with Node.js?

I need to read the value that will bring my query to the database in Oracle, I use oracle DB and node.js.我需要读取将我的查询带到 Oracle 中的数据库的值,我使用 oracle DB 和 node.js。 the oracle DB returns these values in the column that has XML, but the other columns that has normal values return ok. oracle DB 在具有 XML 的列中返回这些值,但具有正常值的其他列返回正常。

{
  "tramaenv": {
    "_readableState": {
      "objectMode": false,
      "highWaterMark": 16384,
      "buffer": {
        "head": null,
        "tail": null,
        "length": 0
      },
      "length": 0,
      "pipes": null,
      "pipesCount": 0,
      "flowing": null,
      "ended": false,
      "endEmitted": false,
      "reading": false,
      "sync": true,
      "needReadable": false,
      "emittedReadable": false,
      "readableListening": false,
      "resumeScheduled": false,
      "emitClose": true,
      "autoDestroy": false,
      "destroyed": false,
      "defaultEncoding": "utf8",
      "awaitDrain": 0,
      "readingMore": false,
      "decoder": null,
      "encoding": null
    },
    "readable": true,
    "_events": {},
    "_eventsCount": 1,
    "_writableState": {
      "corkedRequestsFree": {
        "next": null,
        "entry": null
      }
    },
    "writable": true,
    "allowHalfOpen": true,
    "offset": 1
  }
}

but the real content is this XML但真正的内容是这个 XML

<soapenv:Envelope >
    <soapenv:Header/>
    <soapenv:Body>
        <imp:obtNroConstanciaElectronica>
            <reqObtNroConstanciaElectronica>
                <idecotizacion>12668264</idecotizacion>
                <ideprod>2003</ideprod>
            </reqObtNroConstanciaElectronica>
        </imp:obtNroConstanciaElectronica>
    </soapenv:Body>
</soapenv:Envelope>

If your sample data is:如果您的样本数据是:

CREATE TABLE table_name ( id, xml ) AS
SELECT 1, '<soapenv:Envelope xmlns:soapenv="https://example.com/soapenv" xmlns:imp="https://example.com/imp">
    <soapenv:Header/>
    <soapenv:Body>
        <imp:obtNroConstanciaElectronica>
            <reqObtNroConstanciaElectronica>
                <idecotizacion>12668264</idecotizacion>
                <ideprod>2003</ideprod>
            </reqObtNroConstanciaElectronica>
        </imp:obtNroConstanciaElectronica>
    </soapenv:Body>
</soapenv:Envelope>' FROM DUAL;

(Note: that the namespaces for soapenv and imp are declared in the XML.) (注意: soapenvimp的命名空间在 XML 中声明。)

Then you can use the SQL query:然后可以使用 SQL 查询:

SELECT id,
       x.*
FROM   table_name t
       CROSS APPLY XMLTABLE(
         XMLNAMESPACES(
           'https://example.com/soapenv' AS "soapenv",
           'https://example.com/imp' AS "imp"
         ),
         '/soapenv:Envelope/soapenv:Body/imp:obtNroConstanciaElectronica/reqObtNroConstanciaElectronica'
         PASSING XMLTYPE( t.xml )
         COLUMNS
           idecotizacion NUMBER PATH './idecotizacion',
           ideprod NUMBER PATH './ideprod'
       ) x

And the output is: output 是:

ID ID IDECOTIZACION IDECOTIZACION IDEPROD IDEPROD
1 1 12668264 12668264 2003 2003年

Then you just need to call that query as you would any other SQL query in your node.js script and get the values.然后,您只需像在 node.js 脚本中的任何其他 SQL 查询一样调用该查询并获取值。

The node-oracledb documentation has a whole section on XMLType . node-oracledb 文档有一整节关于 XMLType

It appears that you have successfully fetched as CLOB (which is necessary to avoid the XML length being limited to the maxium VARCHAR2 length), ie.看来您已成功获取为 CLOB(这是避免 XML 长度限制为最大 VARCHAR2 长度所必需的),即。 you've done something like:你做了类似的事情:

const sql = `SELECT XMLTYPE.GETCLOBVAL(res) FROM resource_view`;

You can either stream the LOB , or tell node-oracledb to return it directly as a string .您可以stream LOB或告诉 node-oracledb 直接将其作为 string 返回 To do the latter, add this to the top of your script:要执行后者,请将其添加到脚本的顶部:

oracledb.fetchAsString = [ oracledb.CLOB ];

You should benchmark both solutions and use what's best.您应该对这两种解决方案进行基准测试并使用最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM