正在访问来自 node.js 的 xml 内容

accesing xml content from node.js

我在 MarkLogic 数据库中有一堆 XML 文档。 我正在研究 node.js 中间层,它需要将某个集合中所有 xml 文档的某些属性传递到 JSON 数组中。

到目前为止我有这个:

var marklogic = require('marklogic');
var my = require('./my-connection.js');

var db = marklogic.createDatabaseClient(my.connInfo);
var qb = marklogic.queryBuilder;

db.documents.query(
  qb.where(qb.collection("alert"))
).result( function(documents) {
    console.log('The alerts collection:')
    documents.forEach( function(document) {
      //console.log('\nURI: ' + document.uri);
      var aObj = document.content
      //var alert = aObj.getElementsByTagNameNS("http://example.com/sccs/alert","alert");
      console.log('Alert: ' + document.content);
    });
}, function(error) {
    console.log(JSON.stringify(error, null, 2));
}); 

这让我返回了内容。

示例结果:

Alert: <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0"?>
<obj:object xmlns:obj="http://marklogic.com/solutions/obi/object">
  <obi:metadata xmlns:obi="http://marklogic.com/solutions/obi" createdBy="admin" createdDateTime="2015-09-23T20:42:48.562829Z" lastUpdatedBy="admin" lastUpdatedDateTime="2015-09-23T20:42:48.562829Z"/>
  <obj:label>Active</obj:label>
  <obj:type>alert</obj:type>
  <obj:id>41e718eb-a2e2-49e0-939a-68bb87c1e301</obj:id>
  <obj:content>
    <alert xmlns="http://example.com/sccs/alert">
      <obj:property sourceCount="1">
        <status>Active</status>
      </obj:property>
      <obj:property sourceCount="1">
        <position>{"type":"Point", "coordinates":[52.2, 4.3]}</position>
      </obj:property>
      <obj:property sourceCount="1">
        <scc:id xmlns:scc="http://example.com/sccs">2a65b4cc-acee-4675-a8ba-d8c5dfaac9dd</scc:id>
      </obj:property>
    </alert>
  </obj:content>
  <obj:workspaces>
    <obj:workspace id="Public"/>
  </obj:workspaces>
  <obj:sourceIds count="1">
    <source:id xmlns:source="http://marklogic.com/solutions/obi/source">42aebdc7-41aa-4695-b514-2bb63f85d47c</source:id>
  </obj:sourceIds>
</obj:object>

问题:

我想访问此内容中的 'obj:content' 元素。

在查询控制台中,我可以通过以下方式执行此操作:

var alerts = fn.collection("alert").toArray();
var aXML = alerts[2].getElementsByTagNameNS("http://example.com/sccs/alert","alert");

如何从 node.js 执行此操作?

(我不明白node.js中查询返回的对象类型)

MarkLogic node.js API returns XML 文档为字符串;您需要使用 XML 解析器来查询、投影或处理 XML。 npm 上有很多与 XML 交互的模块,参见

您可能会发现使用 MarkLogic 的服务器端转换更容易,并首先将您的 XML 文档转换为 JSON:http://docs.marklogic.com/guide/node-dev/extensions#id_78179