xml数据
<?xml version="1.0" encoding="UTF-8"?>
<Workflow>
<ID>Workflow-MKRU</ID>
<LocalCachePath>Z:\MAITest\Cache</LocalCachePath>
<SharedCachePath></SharedCachePath>
<Name>Test WF</Name>
<Desc>I'm testing shit</Desc>
<Components>
<Component>
<ID>ImageReader</ID>
<Type>ImageReaderComp</Type>
<Name>ReaderU08</Name>
<Desc></Desc>
<Properties>
<Property Name="Filepath" Type="String">D:\StandardImages\Lena.jpg</Property>
<Property Name="DataType" Type="enum">0</Property>
</Properties>
<InputPorts/>
<OutputPorts>
<Port>
<Name>Image</Name>
<Type>ImageTransport</Type>
<Transport>
<ID>ImageReaderCompJXRX-Image</ID>
<Type>ImageTransport</Type>
<Method>1</Method>
<Location>Z:\ImageReaderCompJXRX-Image</Location>
<Name></Name>
<Desc></Desc>
<SrcComponent>ImageReaderCompJXRX-Image</SrcComponent>
<DstComponents>
<ID>EdgeDetectorCompMWSL-Image</ID>
<ID>ImageDRACompGGWG-InputImage</ID>
</DstComponents>
</Transport>
</Port>
</OutputPorts>
</Component>
<Component>
<ID>ImageReaderComp</ID>
<Type>ImageReaderComp</Type>
<Name>ReaderU16</Name>
<Desc></Desc>
<Properties>
<Property Name="Filepath" Type="String">D:\Lena.jpg</Property>
<Property Name="DataType" Type="enum">1</Property>
</Properties>
<InputPorts/>
<OutputPorts>
<Port>
<Name>Image</Name>
<Type>ImageTransport</Type>
<Transport>
<ID>ImageReaderCompDDIU-Image</ID>
<Type>ImageTransport</Type>
<Method>1</Method>
<Location>Z:\ImageReaderCompDDIU-Image</Location>
<Name></Name>
<Desc></Desc>
<SrcComponent>ImageReaderCompDDIU-Image</SrcComponent>
<DstComponents>
<ID>ImageWriterCompMILV-Image</ID>
</DstComponents>
</Transport>
</Port>
</OutputPorts>
</Component>
</Components>
</Workflow>
代码
files.forEach(function (file) {
var xmlfile = require('fs').readFileSync(directoryPath+file, 'utf8');
var parser = new xml2js.Parser();
parser.parseString(xmlfile,function (err, result) {
var jsonresponse= JSON.stringify(result)
var jsonparsed = JSON.parse(jsonresponse);
console.dir(jsonparsed);
console.dir(jsonresponse);
});
});
那么,这就是我到目前为止所拥有的一切。 我已经尝试了许多xml2js解析器,以使其能够响应两种不同类型的json响应,但是我仍然不知道如何解析出要插入到数据库中所需的方式。 我基本上是在寻找将精选xml数据插入mongoDB的最佳方法。 以下是我的XML数据的当前状态。 主要目标: 我需要将每个组件xml节点的信息获取到mongodb中。
{ Workflow:
{ ID: [ 'Workflow-MKRU' ],
LocalCachePath: [ 'Z:\\MAITest\\Cache' ],
SharedCachePath: [ '' ],
Name: [ 'Test WF' ],
Desc: [ 'I\'m testing shit' ],
Components: [ [Object] ] } }
'{"Workflow":{"ID":["Workflow-MKRU"],"LocalCachePath":["Z:\\\\MAITest\\\\Cache"],"SharedCachePath":[""],"Name":["Test WF"],"Desc":["I\'m testing shit"],"Components":[{"Component":[{"ID":["ImageReader"],"Type":["ImageReaderComp"],"Name":["ReaderU08"],"Desc":[""],"Properties":[{"Property":[{"_":"D:\\\\StandardImages\\\\Lena.jpg","$":{"Name":"Filepath","Type":"String"}},{"_":"0","$":{"Name":"DataType","Type":"enum"}}]}],"InputPorts":[""],"OutputPorts":[{"Port":[{"Name":["Image"],"Type":["ImageTransport"],"Transport":[{"ID":["ImageReaderCompJXRX-Image"],"Type":["ImageTransport"],"Method":["1"],"Location":["Z:\\\\ImageReaderCompJXRX-Image"],"Name":[""],"Desc":[""],"SrcComponent":["ImageReaderCompJXRX-Image"],"DstComponents":[{"ID":["EdgeDetectorCompMWSL-Image","ImageDRACompGGWG-InputImage"]}]}]}]}]},{"ID":["ImageReaderComp"],"Type":["ImageReaderComp"],"Name":["ReaderU16"],"Desc":[""],"Properties":[{"Property":[{"_":"D:\\\\Lena.jpg","$":{"Name":"Filepath","Type":"String"}},{"_":"1","$":{"Name":"DataType","Type":"enum"}}]}],"InputPorts":[""],"OutputPorts":[{"Port":[{"Name":["Image"],"Type":["ImageTransport"],"Transport":[{"ID":["ImageReaderCompDDIU-Image"],"Type":["ImageTransport"],"Method":["1"],"Location":["Z:\\\\ImageReaderCompDDIU-Image"],"Name":[""],"Desc":[""],"SrcComponent":["ImageReaderCompDDIU-Image"],"DstComponents":[{"ID":["ImageWriterCompMILV-Image"]}]}]}]}]}]}]}}'
答案 0 :(得分:0)
在MongoDB中,您不能存储XML数据,需要将它们转换为JSON来存储数据。这是将XML转换为JSON的库:https://www.npmjs.com/package/xml2js
答案 1 :(得分:0)
我必须将其添加到解析的数据中-> var data = {texto:json}; 在找到推荐它的选择之前,一直都在寻找。
const path = require('path');
const directoryPath = path.join(__dirname, '/');
fs.readFile(directoryPath+'/workflow.json', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
var json = JSON.parse(data);
var data = {texto: json};
db.collection("workflows").insert(data, function(err, res)
{
if (err) throw err;
});
});