Having troubles reading from a XML
What I have now:
var file = plugins.file.showFileOpenDialog(1, null, false, ['XML files', 'xml'], null, 'Open XML File');
if (file) {
var txt = plugins.file.readTXTFile(file);
if (txt) {
txt = utils.stringTrim(txt.substring(txt.indexOf('\n')+1));
var xml = new XML(txt);
}
}
Then I have my xml objet.
Now I want to read for example the attribute isStartNewPage
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http:// ………… >
<group name="groupoid" isStartNewPage="true">
How do I do this to get to this attribute and its value?
Regards
ttmgybta
normally you just strip the xml header and turn the string into xml using ‘new XML()’
than you can use the functions found under the JSLIb XML node.
strip xml header:
var $output= txt.replace(/<\/?[^>]+(>|$)/g, function (strMatch, p1){return (/xml version/.test(strMatch)) ? '' : strMatch})
var $xml = new XML(utils.stringTrim($output.substring($output.indexOf('<'))));
then access the xml:
var $group= $xml.jasperReport.group;
application.output($group.attribute('isStartNewPage'));
It is not working as you suggested.
Any other ideas what I’m doing wrong?
My code:
var file = plugins.file.showFileOpenDialog(1, null, false, ['XML files', 'xml'], null, 'Open XML File');
if (file) {
var txt = plugins.file.readTXTFile(file);
if (txt) {
var output= txt.replace(/<\/?[^>]+(>|$)/g, function (strMatch, p1){return (/xml version/.test(strMatch)) ? '' : strMatch})
var xml = new XML(utils.stringTrim(output.substring(output.indexOf('<'))));
var $group= xml.jasperReport.group;
application.output($group.attribute('isStartNewPage'));
}
}
My XML:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport>
<group name="groupoid" isStartNewPage="true">
</group>
</jasperReport>
Hi all:
Normally I use the xml plugin. One example for read one attribute is:
var myXml = '<?xml version="1.0" encoding="UTF-8"?> <jasperReport> <group name="groupoid" isStartNewPage="true"> </group> </jasperReport>'
$xml=plugins.XmlReader.readXmlDocumentFromString(myXml)
$nodes=$xml[0].getChildNodes()
application.output ($nodes[0].getAttributeValue("isStartNewPage"));
Best regards.
Hello,
I think using the JS Lib XML and XMLList is the preferred method in Servoy 4.0 and greater.
Naming the xml object the same as the root element of the xml string, helps keep things straight in my mind ![Smile :)]()
xstr = '<?xml version="1.0" encoding="UTF-8"?><jasperReport><group name="groupoid" isStartNewPage="true"></group></jasperReport>'
xstr = xstr.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "")
var jasperReport = new XML(xstr)
var $group=jasperReport.group;
application.output($group.attribute('isStartNewPage'));