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"]}]}]}]}]}]}]}}'