0
我在MarkLogic数据库中有一堆XML文档。 我正在研究node.js middletier,它需要将某个集合中所有xml文档的某些属性传递给JSON数组。访问来自node.js的xml内容
到目前为止,我有这样的:
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:内容' 在此内容元素。
var alerts = fn.collection("alert").toArray();
var aXML = alerts[2].getElementsByTagNameNS("http://example.com/sccs/alert","alert");
我怎样才能做到这一点从node.js的:
在查询控制台我可以做到这一点?
(我不明白,在node.js中从查询返回的对象类型)
服务器端转换+1。构建一个转换,用于识别您需要的信息,并以JSON形式返回,而不是转换整个文档。 –
我也会+1。 OBI具有一些对象XML到JSON的默认转换,但是您可能想编写自己的代码来执行Dave所说的 - 只返回您想要的数据,并且使用该格式,这对客户端来说是最简单的工作方式。 – rjrudin
是的,我最终使用了服务器端转换 –