Skip to content

Commit

Permalink
handle xsd order correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bgdavidx committed May 12, 2017
1 parent 85a860a commit dd313e8
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions src/parser/xmlHandler.js
Expand Up @@ -104,14 +104,14 @@ class XMLHandler {
val = val.replace("<![CDATA[","");
val = val.replace("]]>","");
element.cdata(val);
}else if(isSimple && typeof val !== "undefined" && val !== null
}else if(isSimple && typeof val !== "undefined" && val !== null
&& typeof val[this.options.xmlKey] !== "undefined") {
val = val[this.options.xmlKey];
val = val[this.options.xmlKey];
element = node.element(elementName);
element.raw(val);
}else {
element = isSimple ? node.element(elementName, val) : node.element(elementName);
}
}

if (xmlns && descriptor.qname.nsURI) {
element.attribute(xmlns, descriptor.qname.nsURI);
Expand Down Expand Up @@ -156,7 +156,7 @@ class XMLHandler {
}
}
}
}
}
}
//val is not an object - simple or date types
if (val != null && ( typeof val !== 'object' || val instanceof Date)) {
Expand All @@ -169,14 +169,14 @@ class XMLHandler {
}
if (nameSpaceContextCreated) {
nsContext.popContext();
}
}
return node;
}

this.mapObject(element, nsContext, descriptor, val, attrs);
if (nameSpaceContextCreated) {
nsContext.popContext();
}
}
return node;
}

Expand Down Expand Up @@ -204,11 +204,14 @@ class XMLHandler {
}

var elements = {}, attributes = {};
var elementsOrder = [];

if (descriptor != null) {
for (let i = 0, n = descriptor.elements.length; i < n; i++) {
let elementDescriptor = descriptor.elements[i];
let elementName = elementDescriptor.qname.name;
elements[elementName] = elementDescriptor;
elementsOrder.push(elementName);
}
}

Expand All @@ -220,24 +223,51 @@ class XMLHandler {
}
}

// handle later if value is an array
let elementIndex = 0;

// handle later if value is an array
if (!Array.isArray(val)) {
const handled = {};

for (let k = 0; k < elementsOrder.length; k++) {
let elementDescriptor = elements[elementsOrder[k]];
let childNode = null;

for (let p in val) {
if (handled[p] || p === this.options.attributesKey)
continue;
if (p === elementDescriptor.qname.name) {
childNode = val[p];
break;
}
}

if (childNode) {
this.jsonToXml(node, nsContext, elementDescriptor, childNode);
handled[elementDescriptor.qname.name] = true;
}
}

for (let p in val) {
if (p === this.options.attributesKey)
if (handled[p] || p === this.options.attributesKey)
continue;
let child = val[p];

let attributeDescriptor = attributes[p];

if (attributeDescriptor == null) {
continue;
let child = val[p];
let childDescriptor = elements[p] || attributes[p];
if (childDescriptor == null) {
if (this.options.ignoreUnknownProperties)
if (this.options.ignoreUnknownProperties)
continue;
else
childDescriptor = new ElementDescriptor(
else
attributeDescriptor = new ElementDescriptor(
QName.parse(p), null, 'unqualified', Array.isArray(child));
}
if (childDescriptor) {
this.jsonToXml(node, nsContext, childDescriptor, child);
}
}
if (attributeDescriptor) {
this.jsonToXml(node, nsContext, attributeDescriptor, child);
handled[p] = true;
}
}
}

this.addAttributes(node, nsContext, descriptor, val, attrs);
Expand Down Expand Up @@ -280,10 +310,10 @@ class XMLHandler {
}
}

static createSOAPEnvelope(prefix, nsURI) {
static createSOAPEnvelope(prefix, nsURI, xsd) {
prefix = prefix || 'soap';
var doc = xmlBuilder.create(prefix + ':Envelope',
{version: '1.0', encoding: 'UTF-8', standalone: true});
{version: '1.0', encoding: 'UTF-8', standalone: true, xsd: xsd });
nsURI = nsURI || 'http://schemas.xmlsoap.org/soap/envelope/'
doc.attribute('xmlns:' + prefix,
nsURI);
Expand Down Expand Up @@ -491,7 +521,7 @@ class XMLHandler {
attrs[a] = xsiType.name;
if(xsiType.prefix){
xsiXmlns = nsContext.getNamespaceURI(xsiType.prefix);
}
}
}
}
let attrName = qname.name;
Expand Down Expand Up @@ -586,7 +616,7 @@ class XMLHandler {
var top = stack[stack.length - 1];
self._processText(top, text);
};

p.ontext = function(text) {
text = text && text.trim();
if (!text.length)
Expand Down Expand Up @@ -730,7 +760,7 @@ function declareNamespace(nsContext, node, prefix, nsURI) {
} else if (node) {
node.attribute('xmlns:' + mapping.prefix, mapping.uri);
return mapping;
}
}
return mapping;
}

Expand Down

0 comments on commit dd313e8

Please sign in to comment.