а

Dhtmlxajax receive xml cdata


#1

Hi,

I don’t think this is the right thread, but I don’t know where to put it otherwise.

I’m using dhtmlxajax version 4.1.2.

I’m trying to get data from the server via dhtmlxajax using following code:

function ajaxRequest(url) {
	window.dhx4.ajax.get(url, ajresponse);
}
function ajresponse(r){
	var xml = r.xmlDoc.responseXML;
	if (xml != null) {
	        // response successfuly parsed
	        var root = xml.getElementsByTagName("response")[0];
	        var day=root.getAttribute("day");
	        var error=root.getAttribute("error");
	        var dataroot=xml.getElementsByTagName("data")[0];
	        var data=dataroot.getAttribute("cdata");
	        //... some other code
	} else {
		// response is not in xml format
	}
}

This is an example of the data I get back from the server:

<?xml version="1.0" ?><response day="2015-02-16" error="0" /><data><![CDATA[' <table><tr><td style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 11px;'><acronym title='01/02/2015'>Test</acronym></td></tr><tr><td style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 11px;'><acronym title=''></acronym></td></tr></table>']]></data>

I need to get the data from response (day, error) and also from data (cdata) and I need to put them into different variables (day, error and data).

How do I do this?


#2

XML which you are using is not well-formed. You have 2 top level tags

In case of valid xml, code as follows can be used

<script type="text/javascript"> dhx4.ajax.get("test.xml?12", function(r){ var response = dhx4.ajax.xpath("//response", r.xmlDoc.responseXML)[0]; alert(response.getAttribute("day")) }); </script>


#3

Ok that works, thx!

I corrected my xml to this:

<?xml version="1.0" ?><response day="2015-02-16" error="0"><![CDATA[' <table><tr><td style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 11px;'><acronym title='01/02/2015'>Test</acronym></td></tr><tr><td style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 11px;'><acronym title=''></acronym></td></tr></table>']]></response>

How do I read the cdata? Or do i need to put it like this:

... <data><![CDATA[' <table>...</table>']]></data></response>

I need to get the cdata…


#4

Can anyone explain me how to get the cdata back via xml?
Is my xml wrong or am I using the wrong code? There isn’t much documentation online about this… :frowning:


#5

You can use

response.firstChild.nodeValue

#6

Thx! That worked!

Here is the code I use for future reference and to help someone else who is struggling with the same problem:
xml:

[code]<?xml version="1.0" ?>

<![CDATA[
16/02/2015
17/02/2015
]]>

[/code]

Javascript:

function byId(id) {
	return document.getElementById(id);
}
function ajaxRequest(url,data) {
	url = url + "?"+data;
	dhx4.ajax.get(url, getResponse);
}
function getResponse(r) {
	var xml = r.xmlDoc.responseXML;
	var res = byId("result");
 	if (xml != null) {
		// response successfuly parsed
		var response = xml.getElementsByTagName("response")[0];
		var error = response.getAttribute("error");
		var html= "";
		if (error=="0"){
			html=response.firstChild.nodeValue;
		}
		else{
			html="There was an error.";
		}
		res.innerHTML=html;
 	}
 	else {
		// response is not in xml format
		res.innerHTML="Result not in xml format.";
	}
}

This is filled with the value from res.innerHTML, just put it somewhere in the body:

<div id='res'<div>