0

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

2 回答 2

0

在 MongoDB 中,您不能存储 XML 数据,您需要将它们转换为 JSON 来存储您的数据。这是一个将 XML 转换为 JSON 的库:https ://www.npmjs.com/package/xml2js

于 2019-02-26T22:01:52.970 回答
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;
            });

        });
于 2019-02-27T22:13:14.287 回答