Results 1 to 7 of 7

Thread: AjxUtil.xmlToJs can't handle namespaces

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Urmond, Netherlands
    Posts
    51
    Rep Power
    10

    Default AjxUtil.xmlToJs can't handle namespaces

    The AjxUtil.xmlToJs method can't handle namespaces in attribute tags.

    The response of a SOAP request:
    Code:
    <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <env:Body>
        <n1:FindAllProductsResponse xmlns:n1="urn:ActionWebService" env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
          <return n2:arrayType="xsd:int[4]" xmlns:n2="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="n2:Array">
            <item>
              1
            </item>
            <item>
              2
            </item>
            <item>
              3
            </item>
            <item>
              4
            </item>
          </return>
        </n1:FindAllProductsResponse>
      </env:Body>
    </env:Envelope>
    The result after it is been processed by AjxUtil.xmlToJs (I stripped the whitespaces where I also are having problems with):
    Code:
    {
    	Body:{
    		FindAllProductsResponse: {
    			xmlns:n1:"urn:ActionWebService",
    			env:encodingStyle: "http:\/\/schemas.xmlsoap.org\/soap\/encoding\/",
    			return:{
    				n2:arrayType:"xsd:int[4]",
    				xmlns:n2:"http:\/\/schemas.xmlsoap.org\/soap\/encoding\/",
    				xsi:type:"n2:Array",
    				"",
    				item:{
    					"1"
    				},
    				item:{
    					"2"
    				},
    				item:{
    					"3"
    				},
    				item:{
    					"4"
    				}
    			}
    		}
    	}
    }
    If you use the eval function on this then it obviously brakes on xmlns:n1 and so on.

    I wrote a little fix:
    Code:
    AjxUtil.xmlToJs =
    function(node, omitName) {
    
    	if (node.nodeType == AjxUtil.TEXT_NODE)
    		return ['"', node.data, '"'].join("");
    
    	var name = node.name ? node.name : node.localName;
    	if (node.nodeType == AjxUtil.ELEMENT_NODE) {
    		var text = omitName ? "{" : [name, ":{"].join("");
    		var needComma = false;	
    		if (node.attributes ) {
    			for (var i = 0; i < node.attributes.length; i++) {
    				var attr = node.attributes[i];
    				if (attr.name == "xmlns") continue;
    				if (needComma) text += ",";
    				var value = AjxUtil.isNumeric(attr.value) ? attr.value : AjxUtil.jsEncode(attr.value);
    				var attr_name = attr.name
    				var indexdblp = attr_name.indexOf(":")
    				if(indexdblp != -1 ) attr_name = attr_name.substr(indexdblp+1)
    
    				text = [text, attr_name, ':', value].join("");
    				needComma = true;
    			}
    		}
    		if (node.hasChildNodes()) {
    			
    			var cnodes = new Object();
    			var hasChild = false;
    			for (var i = 0; i < node.childNodes.length; i++) {
    				var child = node.childNodes[i];
    				var cname = child.name ? child.name : child.localName;
    	
    				var isAttr = AjxUtil.NODE_IS_ATTR[cname] || 
    							 (name == "content" && parent.name == "note");
    							 
    				if (isAttr) {
    					if (needComma) text += ",";
    					text = [text, cname, ':', AjxUtil.jsEncode(child.textContent)].join("");
    					needComma = true;
    				} else {
    					if (!cnodes[cname])
    						cnodes[cname] = new Array();
    					cnodes[cname].push(child);
    					hasChild = true;
    				}
    			}
    			if (hasChild && needComma) {text += ","; needComma = false;}
    			for (var cname in cnodes) {
    				
    				if (needComma) {
    					text += ",";
    					needComma = false;
    				}
    				var repeats = AjxUtil.NODE_REPEATS[cname] ||
    							  (cname == "mp" && name == "mp");
    				if (repeats) text += cname + ":[";
    				var clist = cnodes[cname];
    				for (var i = 0; i < clist.length; i++) {
    					if (needComma) text += ",";
    					text += AjxUtil.xmlToJs(clist[i], repeats);
    					needComma = true;
    				}
    				if (repeats) text += "]";
    			}
    		}
    		text += "}";
    	}
    
    	return text;
    }
    Somewhere in the code there are the lines:
    var indexdblp = attr_name.indexOf(":")
    if(indexdblp != -1 ) attr_name = attr_name.substr(indexdblp+1)

    text = [text, attr_name, ':', value].join("");

    They take care of the namespaces by removing them.

  2. #2
    Join Date
    Sep 2005
    Posts
    14
    Rep Power
    10

    Default

    I created Web Services in Ms.Net and the result return as below:

    <?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>
    <getMasterFilesResponse content="[{&quot;IdentityID&quot;:1,&quot;Code&quot;:&quot;S LS&quot;,&quot;MyName&quot;:&quot;Sales Invoice&quot;},{&quot;IdentityID&quot;:2,&quot;Cod e&quot;:&quot;PYM&quot;,&quot;MyName&quot;:&quot;P ayment Invoice&quot;}]" xmlns="http://www.profrtgroup.com/webservices/" />
    </soap:Body>
    </soap:Envelope>

    it failed to create xmldoc from the above message.
    Then i remove <?xml version="1.0" encoding="utf-8"?>, it success....

    This occurs only in IE, Firefox is fine.

    Help please...

  3. #3
    Join Date
    Oct 2005
    Location
    Urmond, Netherlands
    Posts
    51
    Rep Power
    10

    Default

    What kind of error do you get when you try with the header?

  4. #4
    Join Date
    Sep 2005
    Posts
    14
    Rep Power
    10

    Default

    This will not cause an error from CreateXmlFromString
    But error occurred in _check function.

    the doc will retrun with 2 childnodes...
    one for <?xml .... ?>
    and another one for <soap:Envelope ....>

    _check function will check for only valid return is only one childnode for doc.

  5. #5
    Join Date
    Oct 2005
    Location
    Urmond, Netherlands
    Posts
    51
    Rep Power
    10

    Default Whitespaces

    I also had problems with the _check method. Because the AJAX xml methods donīt ignore whitespaces there are more elements in the xml document then _check expects. I already opend a tread about that problem.

    Maybe you can right a fix for the problem and share the code with us.

  6. #6
    Join Date
    Sep 2005
    Posts
    14
    Rep Power
    10

    Default

    The main issue for me is the problem occurs only in IE.

Similar Threads

  1. how to get zimbra to handle local system mail
    By fisch09 in forum Administrators
    Replies: 13
    Last Post: 10-21-2010, 05:53 AM
  2. How to handle SOAP body response in PHP
    By ferdinant in forum Developers
    Replies: 1
    Last Post: 07-03-2007, 02:21 PM
  3. Need separate server to handle mail that Zimbra doesn't
    By danfluidmind in forum Administrators
    Replies: 2
    Last Post: 06-02-2007, 12:37 PM
  4. Phone zimlet and callto handle
    By rickard in forum Users
    Replies: 3
    Last Post: 06-01-2006, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •