Page 1 of 2

How to import data from an XML file?

PostPosted: Mon Aug 16, 2010 11:24 pm
by nromeou
Hi,
i want to import data from an xml file and im not beign able to import the data.
Im trying to use the XMLreader plugin but im not getting how to use the nodes it returns.

Can anyone give me some explanation of how to do this??

thanks in advance

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 9:38 am
by pbakker
I'd use the JavaScript XML implementation (see XML and XMLList under JS Lib): more powerfull.

Just read the file using the file plugin, create an XML object form the contents of the file (assuming it's valid XML) and process it to your liking.

To create an XMl object from a string: var x = new XML('<myXMLStringHere/>')

Note that if your XML file starts with something like "<?xml version="1.0" encoding="UTF-8"?>", you need to first strip it off, because the JavaSCript XMl implementation doesn't consider that valid XML.

Paul

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 10:11 am
by mboegem
pbakker wrote:Note that if your XML file starts with something like "<?xml version="1.0" encoding="UTF-8"?>", you need to first strip it off, because the JavaSCript XMl implementation doesn't consider that valid XML.


which can be easily done using replace like this.
Code: Select all
var $xml = myXmlString;
$xml = $xml.replace(/<\/?[^>]+(>|$)/g, function (strMatch, p1){return (/xml version/.test(strMatch)) ? '' : strMatch})

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 11:26 am
by rioba
I too had to import data from an xmL file recently. I tried to use the XML plugin (i did not know the Javascript XML implementation) but it is confusing and isn't flexible enough (by the way: why having a XML plugin in Servoy when JavaScript can be used instead?). Anyway, if you want to see an example, I posted some code here http://www.servoy.com/forum/viewtopic.php?f=4&t=14501. The original file was a list of ISO regional and country codes.

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 11:36 am
by pbakker
The XML plugin was there before the XML implementation in JavaScript was there.

Paul

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 1:10 pm
by Kahuna
Paul - any chance we could get some 'How To's on reading and writing XML using both methods (Plugin and Java) - in addition to the Wiki refrences?

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 2:36 pm
by pbakker
If you google for "e4x", which is the official name for the JavaScript XML implementation, you'll find quite a bit of information.

Paul

Re: How to import data from an XML file?

PostPosted: Tue Aug 17, 2010 4:08 pm
by Kahuna
pbakker wrote:If you google for "e4x", which is the official name for the JavaScript XML implementation, you'll find quite a bit of information.

Paul


Cheers M8y

Re: How to import data from an XML file?

PostPosted: Tue Nov 16, 2010 12:32 am
by mboegem
I've been trying to get the content of the attached zipfile imported as XML with E4X.
I imported this as textfile (utf-8 encodeing) and then tried to get rid of the xml-header.
But in some way I just can't seem to get it working...
Probably I'm missing a small step, but what... (it shouldn't be so hard :-( )

Any advise?

Re: How to import data from an XML file?

PostPosted: Tue Nov 16, 2010 8:24 am
by ptalbot
Hi Marc,

first when you create files with UTF-8, always make sure your editor doesn't output UTF BOM, because most of the time Java doesn't like it...
UTF Byte Order Mark are the 3 first bytes inserted in UTF-8 files so that they are recognized as such (EF BB BF). You can open you file with an HexaDecimal editor and you will see that they are present in your xml file (although invisible).
Most text/xml editors will offer an option to strip these bytes, unfortunately NotePad on Windows doesn't, so use a real editor ;)

That being said, as you have guessed, you need to strip the xml declaration from your file.
If you do so, you shouldn't have any problem using it with E4X in Servoy...

I just tried it, using a simple test method like this:
Code: Select all
function testE4X() {
   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);
         application.output(xml.salesorder);
      }
   }
}


Works fine here!

Re: How to import data from an XML file?

PostPosted: Tue Nov 16, 2010 10:39 am
by mboegem
Hi Patrick,

As I said... we were close to the key: the trim function was the actual key to get it working...
We use kind of the same code you posted, except that we don't use '\n' to strip the header, as it might be a file consisting of just 1 line...

Thnkx for your help, much appreciated!

Re: How to import data from an XML file?

PostPosted: Mon Nov 14, 2011 4:08 am
by rogel
mboegem wrote:
pbakker wrote:Note that if your XML file starts with something like "<?xml version="1.0" encoding="UTF-8"?>", you need to first strip it off, because the JavaSCript XMl implementation doesn't consider that valid XML.


which can be easily done using replace like this.
Code: Select all
var $xml = myXmlString;
$xml = $xml.replace(/<\/?[^>]+(>|$)/g, function (strMatch, p1){return (/xml version/.test(strMatch)) ? '' : strMatch})


Hi Marc,

Did you come encounter any slowdown if the string is big(e.g. length = 3586241)?

Re: How to import data from an XML file?

PostPosted: Mon Nov 14, 2011 10:53 am
by mboegem
I don't think I ran into numbers of this size, so I can't really tell.
It wouldn't surprise me, as this function will scan the complete XML for the matching pattern.

Maybe you can look for a way that it will stop scanning as soon as it finds the first match.

Re: How to import data from an XML file?

PostPosted: Mon Nov 14, 2011 8:43 pm
by david
I feel a slight breeze. Must be Monday.

Re: How to import data from an XML file?

PostPosted: Tue Jun 18, 2013 6:19 am
by siavash.roozbehan
Hi guys,

I am using xml files to backup data in our application. Basically I backup each table into an xml file and restoring them by reading the xml files and inserting data into the tables. It works fine for everything except for our documents table which usually contains data > 50Mb. When I try to restore data, I read the xml file belong to each table into an string by using it2be file plugin and then I make an xml object of that string using:
var newXml = new XML(myXmlFileString);

When the program wants to make an object of a string of that size (50MB) it throws a java heap stack memory exception! I was wondering if there is any size limitation for the xml object? If so, how can I deal with this problem?

Cheers,
Siavash