xml string to javascript object

Hi!

Is there a method that automatically converts the XML String to Javascript Objects?

XML inside JavaScript in Servoy is a first class citizen: var x = new XML(‘hello’);

See the XML and XMLList nodes under the JSLib node of the Solution Explorer, or just try the use Code cOmpletion on the variable ‘x’ in the example above.

Paul

Hi Rogel,

Are you talking about serialization like how JSON works ? Or do you just want to parse the XML inside JavaScript?
If it’s the latter then you can do what Paul explained. For the serialization option I don’t think there is support for that in Servoy (perhaps in pure java). You might want to file a feature request for this if you need this.

Hope this helps.

ROCLASI:
Hi Rogel,

Are you talking about serialization like how JSON works ? Or do you just want to parse the XML inside JavaScript?
If it’s the latter then you can do what Paul explained. For the serialization option I don’t think there is support for that in Servoy (perhaps in pure java). You might want to file a feature request for this if you need this.

Hope this helps.

I think it is parsing the XML. Actually, I am moving my code from the web service to our internal solution. I passed an XML data to the web service and it was automatically transformed into Javascript objects. Therefore I made methods inside my web service with javascript object as input. Now since it not be a web service, my method receives an XML string. My problem is if I cannot transform the XML to javascript object automatically then I would need to manually parse it to a javascript object before processing.

pbakker:
XML inside JavaScript in Servoy is a first class citizen: var x = new XML(‘hello’);

See the XML and XMLList nodes under the JSLib node of the Solution Explorer, or just try the use Code cOmpletion on the variable ‘x’ in the example above.

Paul

Thanks Paul!

I have this function that loops though an Object in search of the columnid. Is there a similar implementation if data was an XML Object?

function search() {
	var mydata = new Object();
	mydata.id = 123
	mydata.name = "rogel"
	var columnid = "name"
	var myvalue = getValue(columnid, mydata); //this will get "rogel"
}

function getValue(columnid, data) {
	var value = null;
	for(var curElement in data)
	{ 
		if (data[curElement] instanceof String) {
			if (curElement.toLowerCase().equals(columnid)) {
				value = data[curElement];
				break;
			}
			
		}
	}
	return value;
}

rogel:
Is there a similar implementation if data was an XML Object?

Yes, you can also iterate over XML. Here is a tutorial about XML in javascript that explains it: E4X [rephrase.net]

Joas:

rogel:
Is there a similar implementation if data was an XML Object?

Yes, you can also iterate over XML. Here is a tutorial about XML in javascript that explains it: E4X [rephrase.net]

Thanks for the help. I used the elements() and the name().localName to get its name.

for each (var item in data.elements()) {
	var info = item.name().localName; 
}
Rogel

Is there a way that I will be able to get the value of “fName” without worrying lower and upper case? I do not get a result if I use message…name.fname

Thanks!