I am sorry, this is probably very simple.
I am trying to pass some fields into my solution from an email and naively thought the XML reader would do the job easily. Two hours later! I must be doing something very wrong:
var xmlString = '<firstname="Test"><lastname="Test2">';
var myXML = plugins.XmlReader.readXmlDocumentFromString(xmlString);
How do i read the element ‘firstname’?
I assumed
var firstname=myXML[0].getAttributeValue('firstname')
but all i get is errors.
I have looked for a simple example, but cant find one anywhere. The manual appears as clear as mud and just gives errors.
Can anyone help?
David
Not sure what errors you are getting, but your example string isn’t valid XML.
You could do one of the following for input XML:
<name first='Test' last='Test2'/>
or
<name>
<first>Test</first>
<last>Test2</last>
</name>
Thanks Greg,
How do i return say that value of ‘Firstname’ in the code example?
I dont seem to be able to get the getattribute syntax correct?
var xmlString = '<name><firstname>First</firstname><lastname>Last</lastname></name>';
var myXML = plugins.XmlReader.readXmlDocumentFromString(xmlString);
var firstaname=?
Thanks Again. Some things look and should be so easy, but are not.
David
Hi David,
Attributes are the parameter pairs inside XML tags.```
Text values are the values between the tags.```
<name><firstname>First</firstname><lastname>Last</lastname></name>
Now to get the first name from your XML your code will look something like this:
var xmlString = '<name><firstname>First</firstname><lastname>Last</lastname></name>';
// read XML string and get all root nodes in an Array
var myXML = plugins.XmlReader.readXmlDocumentFromString(xmlString);
// get child nodes (in an Array) from the first root node (which is <name>)
var aNodes = myXML[0].getChildNodes();
// get the text value of the first node (which is <firstname>)
var sFirstName = aNodes[0].getTextValue();
application.output(sFirstName);
Hope this helps.
Note that this is going to get dramatically easier in Servoy 4, with support for E4X in the the JavaScript interpreter.
http://www.w3schools.com/e4x/e4x_example.asp
With E4X, you could do…
var name = <name><first>Test</first><last>Test2</last></name>;
var firstName = name.first;
var lastName = name.last;
Very cool stuff for XML manipulation.