small XML Parser
Last active
September 5, 2016 16:17
-
-
Save erickeno/388628cb781eb375aa7675d526e4a34f to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>XML Parser</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parse = require("xml-parser"); | |
var XMLWriter = require('xml-writer'); | |
var _ = require('underscore'); | |
var obj = parse("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><TransmitResponse xmlns=\"https://services.testingurl.com/LeadService/\"><TransmitResult><Response xmlns=\"\"><Xml status=\"True\" /><Lim status=\"True\" /><Log status=\"True\" /></Response></TransmitResult></TransmitResponse></soap:Body></soap:Envelope>"); | |
function toXml(obj) { | |
console.log(obj) | |
xw = new XMLWriter; | |
xw.startDocument() | |
.startElement('Result'); | |
_.each(obj,function(value, key, list){ | |
return xw.writeElement(`${key}`, `${value}`) | |
}); | |
console.log(xw.toString()); | |
}; | |
if(obj.root !== undefined){ | |
var val = obj.root.children[0].children[0].children[0].children[0].children; | |
if(val && typeof val === 'object'){ | |
var emptyObj = {}; | |
val.forEach(function(v){ | |
return emptyObj[v.name] = v.attributes.status; | |
}); | |
toXml(emptyObj); | |
}else{ | |
console.log('Error', 'invalid val') | |
}; | |
} else { | |
console.log('Error','invalid obj') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"babel-runtime": "6.9.2", | |
"xml-parser": "1.2.1", | |
"xml-writer": "1.6.0", | |
"underscore": "1.8.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _typeof2 = require('babel-runtime/helpers/typeof'); | |
var _typeof3 = _interopRequireDefault(_typeof2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var parse = require("xml-parser"); | |
var XMLWriter = require('xml-writer'); | |
var _ = require('underscore'); | |
var obj = parse("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><TransmitResponse xmlns=\"https://services.testingurl.com/LeadService/\"><TransmitResult><Response xmlns=\"\"><Xml status=\"True\" /><Lim status=\"True\" /><Log status=\"True\" /></Response></TransmitResult></TransmitResponse></soap:Body></soap:Envelope>"); | |
function toXml(obj) { | |
console.log(obj); | |
xw = new XMLWriter(); | |
xw.startDocument().startElement('Result'); | |
_.each(obj, function (value, key, list) { | |
return xw.writeElement('' + key, '' + value); | |
}); | |
console.log(xw.toString()); | |
}; | |
if (obj.root !== undefined) { | |
var val = obj.root.children[0].children[0].children[0].children[0].children; | |
if (val && (typeof val === 'undefined' ? 'undefined' : (0, _typeof3.default)(val)) === 'object') { | |
var emptyObj = {}; | |
val.forEach(function (v) { | |
return emptyObj[v.name] = v.attributes.status; | |
}); | |
toXml(emptyObj); | |
} else { | |
console.log('Error', 'invalid val'); | |
}; | |
} else { | |
console.log('Error', 'invalid obj'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment