Is it possible with Javascript (or jQuery to make example easier) get boolean variable from some field in XML?
If so – how it should be written in XML and what actions should be made on client side?
Please check the following code:
$(document).ready(function(){
var data = "<?xml version='1.0' encoding='utf-8' ?><data><event id='2'><text><![CDATA[Hello there]]></text><bool1>true</bool1><bool2>false</bool2></event></data>";
var xmlDoc = $.parseXML(data);
var $xml = $(xmlDoc);
var bool = data.find("bool1").text(); // "true" - string
});
What I would like here is to get string for the ‘test’ field and boolean for ‘bool1’/’bool2’.
Thank you.
Update: So one either have to set up XML scheme or simply make direct comparisons ( === ‘false’ / === ‘true’ ) and convert that way to boolean.
Compare the read in text to the string “true”, and assign the results of that comparison into your variable.
In case of an extra whitespace in the text node, you could use jQuery to trim the read in text:
If the node holds the text “true” (with some possible surrounding whitespace), then the variable will receive the boolean value of
true. If the node holds ANY value other than the string “true”, the variable will receive the boolean value offalse.