xml data
<?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>
code
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"]}]}]}]}]}]}]}}'