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
是的,我最終使用了服務器端轉換 –