How to import data from an XML file?

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

How to import data from an XML file?

Postby nromeou » Mon Aug 16, 2010 11:24 pm

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
nromeou
 
Posts: 215
Joined: Fri Sep 18, 2009 8:38 pm
Location: Montevideo, Uruguay

Re: How to import data from an XML file?

Postby pbakker » Tue Aug 17, 2010 9:38 am

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
pbakker
 
Posts: 2817
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: How to import data from an XML file?

Postby mboegem » Tue Aug 17, 2010 10:11 am

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})
_____________________
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance SAN Developer

Image
User avatar
mboegem
 
Posts: 1382
Joined: Sun Oct 14, 2007 1:34 pm
Location: Hoofddorp, The Netherlands

Re: How to import data from an XML file?

Postby rioba » Tue Aug 17, 2010 11:26 am

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.
rioba
 
Posts: 242
Joined: Mon Aug 08, 2005 4:26 pm

Re: How to import data from an XML file?

Postby pbakker » Tue Aug 17, 2010 11:36 am

The XML plugin was there before the XML implementation in JavaScript was there.

Paul
pbakker
 
Posts: 2817
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: How to import data from an XML file?

Postby Kahuna » Tue Aug 17, 2010 1:10 pm

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?
(Servoy Version: 6.0.7 Win XP / 7 - SQL Server 2008 R2)
Ian Cordingley (Kahuna)
Kahuna
 
Posts: 1235
Joined: Thu Oct 26, 2006 1:39 am
Location: 1/2 NE UK 1/2 Olvera Spain

Re: How to import data from an XML file?

Postby pbakker » Tue Aug 17, 2010 2:36 pm

If you google for "e4x", which is the official name for the JavaScript XML implementation, you'll find quite a bit of information.

Paul
pbakker
 
Posts: 2817
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: How to import data from an XML file?

Postby Kahuna » Tue Aug 17, 2010 4:08 pm

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
(Servoy Version: 6.0.7 Win XP / 7 - SQL Server 2008 R2)
Ian Cordingley (Kahuna)
Kahuna
 
Posts: 1235
Joined: Thu Oct 26, 2006 1:39 am
Location: 1/2 NE UK 1/2 Olvera Spain

Re: How to import data from an XML file?

Postby mboegem » Tue Nov 16, 2010 12:32 am

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?
Attachments
sales_order.zip
(403 Bytes) Downloaded 126 times
_____________________
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance SAN Developer

Image
User avatar
mboegem
 
Posts: 1382
Joined: Sun Oct 14, 2007 1:34 pm
Location: Hoofddorp, The Netherlands

Re: How to import data from an XML file?

Postby ptalbot » Tue Nov 16, 2010 8:24 am

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!
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
http://www.servoy-stuff.net
https://www.servoyforge.net
--------------------------------------------
Servoy 5.2.16 / 6.0.9 / 6.1.6 / 7.3.1
All OSes / Java 5 & 6 & 7
User avatar
ptalbot
 
Posts: 1612
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: How to import data from an XML file?

Postby mboegem » Tue Nov 16, 2010 10:39 am

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!
_____________________
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance SAN Developer

Image
User avatar
mboegem
 
Posts: 1382
Joined: Sun Oct 14, 2007 1:34 pm
Location: Hoofddorp, The Netherlands

Re: How to import data from an XML file?

Postby rogel » Mon Nov 14, 2011 4:08 am

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)?
rogel
 
Posts: 264
Joined: Mon Jul 04, 2011 9:09 am

Re: How to import data from an XML file?

Postby mboegem » Mon Nov 14, 2011 10:53 am

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.
_____________________
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance SAN Developer

Image
User avatar
mboegem
 
Posts: 1382
Joined: Sun Oct 14, 2007 1:34 pm
Location: Hoofddorp, The Netherlands

Re: How to import data from an XML file?

Postby david » Mon Nov 14, 2011 8:43 pm

I feel a slight breeze. Must be Monday.
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1723
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: How to import data from an XML file?

Postby siavash.roozbehan » Tue Jun 18, 2013 6:19 am

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
Siavash

TSM (The Service Manager)
Australia
siavash.roozbehan
 
Posts: 97
Joined: Wed Apr 11, 2012 2:21 am

Next

Return to How To

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron