-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_xml2js.js
49 lines (47 loc) · 1.38 KB
/
test_xml2js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
/*
This module provides the xml2js based load/save functions for testing.
*/
const fs = require('fs');
const xml2js = require('xml2js');
const xml = require('../xml-csharp-cereal');
// Builder options for CRLF = {renderOpts:{'pretty': true,'newline': '\r\n'}}
module.exports.SaveToXml = function(obj, XmlFactory, fname, opts)
{
return new Promise((resolve,reject)=>
{
var xml_obj = XmlFactory.to_xml2js(obj, opts);
var builder = new xml2js.Builder();
var xml_data = builder.buildObject(xml_obj);
fs.writeFile(fname, xml_data, function(err)
{
if(err) reject(err);
else resolve();
});
});
}
module.exports.LoadFromXml = function(XmlFactory, fname, opts)
{
return new Promise((resolve,reject)=>
{
fs.readFile(fname, function(err, xml_data)
{
var parser = new xml2js.Parser();
if (err) reject(err);
else parser.parseString(xml_data, function (err, xml_obj)
{
if (err) reject(err);
else
{
var obj = null;
try
{
obj = XmlFactory.from_xml2js(xml_obj, opts);
resolve(obj);
}
catch(e) { reject(e); }
}
});
});
});
}