🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.xml
Article #715

converting data types from xml to java

#715
From: Hugo Bouckaert
Date: Thu, 04 Jan 2001 04:20
78 lines
3737 bytes
Hi 

I would like to read in data from an xml document into java storing them
as doubles values into an array.  

I got as far as being able to read in the data from an xml file into a
string variable. Below is the code that does this: it establishes
whether the nodelist is not empty, then finds the xml element
"coordinates", gets rid of any space and endline characters, puts the
characters I need in the string variable "coordinates" and print out the
value of "coordinates" to see if they have been read in properly:   

NodeList toplist = doc.getElementsByTagName( "import-file" );
int lengthOflist = toplist.getLength();

if (lengthOflist > 0){
  Node firstNode = toplist.item(0);
    if( firstNode.getNodeType() == Node.ELEMENT_NODE ){
     
        NodeList coordinateList = doc.getElementsByTagName(
"coordinates" );
        Node coordinateNode = coordinateList.item(0);

        NodeList listcoordinates = coordinateNode.getChildNodes();

        String coordinates = null;
                for (int i = 0 ; i < listcoordinates.getLength() ; i ++
){
                        String value1 =
((Node)listcoordinates.item(i)).getNodeValue().trim();
                                if( value1.equals("") ||
value1.equals("\r") ){
                                continue; //keep iterating
                                }
                                else{
                                coordinates = value1;
                                System.out.println ("Coordinates: " +
coordinates);
                                } //end else
                               } //end for

Note that if I print this out there are several values as the xml tag
"coordinates" contains several comma separated values eg: 

<coordinates>
0.000, 0.500, 0.000, 0.500
</coordinates>

However, what I need is not a string variable containing several
numbers, but an
array of doubles i.e. the string variable coordinates should really be
an array of doubles with each value (0.00, 0.500 etc) stored separately
as an array
variable.  So I was thinking of using StringTokenizer to convert the
values from the xml file to doubles, but I am starting to suspect that
there must be pre-existing methods for
this and that I am probably trying to re-invent the wheel. Reading on
the web I found use of a class "DataConverter" in a tutorial example,
but I can't seem to find the package with the jar file that contains the
class "DataConverter" anywhere. 

Does anyone know of the class DataConverter or another method to convert
xml data into particular data types in java?  If anyone has done this, a
code example would be most welcome. 

Any help will be greatly appreciated. 

Thanks

Hugo 

-- 
Dr Hugo Bouckaert
R&D Support Engineer, Fractal Graphics 
39 Fairway, Nedlands Western Australia 6009
Tel: +618 9386 7917
Email:hugo@fractalgraphics.com.au
Web: http://www.fractalgraphics.com.au
Technical Details
Message-ID: <3A53F9FB.B04AA8D1@fractalgraphics.com.au>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!news.man.torun.pl!news.man.poznan.pl!news-fra1.dfn.de!fu-berlin.de!newsfeed.iinet.net.au!news.iinet.net.au!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!news.man.torun.pl!news.man.poznan.pl!news-fra1.dfn.de!fu-berlin.de!newsfeed.iinet.net.au!news.iinet.net.au!not-for-mail
Message-ID: <3A53F9FB.B04AA8D1@fractalgraphics.com.au>
Date: Thu, 04 Jan 2001 12:20:11 +0800
From: Hugo Bouckaert <hugo@fractalgraphics.com.au>
X-Mailer: Mozilla 4.61 [en] (WinNT; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.lang.java.tech,com.lang.java.help,comp.lang.java.misc,comp.lang.java.api,comp.text.xml,microsoft.public.xml,pl.comp.xml
Subject: converting data types from xml to java
Lines: 78
NNTP-Posting-Host: 203.59.23.78
X-Trace: news.iinet.net.au 978581938 2698 203.59.23.78
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit