?
Solved

eurofxref parse xml using j2ee tecnologies

Posted on 2003-11-17
10
Medium Priority
?
371 Views
Last Modified: 2012-06-21
Hi!

I have wasted many hours and days now trying to figure out how to grab the currency rates off of ECB's xml-page: http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

What I want to do is to get the SEK and USD currency values either into a JSP variable. How can i parse xml using j2ee libraries to obtain these values?
Any help would be very appreciated!

Thanks a lot!
0
Comment
Question by:asolmo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:krispols
ID: 9763700
hello asolmo,

Use JDOM.
http://www.jdom.org/

regards
0
 

Author Comment

by:asolmo
ID: 9763913
Thanks, krispols, but i don't know how can i get a rate value depend on an specific currency value (Have you seen the xml?).

Can you help me?

Thanks
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9764296
>> How can i parse xml using j2ee libraries to obtain these values?
1. You need an JAXP compliant xml parser, be it SAX or DOM. If your server is j2ee 1.3 compliant, it comes with xml parser. Otherwise, you'll need to find one, I prefer apache xerces parser: http://xml.apache.org/xerces2-j/index.html

2. you need to learn how to parse an xml using JAXP. there is some sample code if you download xerces, look into dom.GetElementsByTagName sample and you'll get some idea of how to get data using DOM.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:asolmo
ID: 9765693
Thanks, but the problem is that tags in XML aren't <attribute></attribute>. They are <currency = "XXX" rate = "YYY">, and i don't know how to obtain the rate value depend on the currency value

Thanks
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9766533
loop through all the Cube tags and check the attributes to find what you want. can you post the code you have so we can help you from there.
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9767358
This link has something that you are asking for

http://www.devshed.com/Server_Side/XML/SAXandXerces/SAXandXerces1/page6.html

these are the areas of intrest
XML ->
<cost currency="USD">2.99</cost>

and in java sax parser

            String AttributeName,AttributeType,AttributeValue = "";
            for (int i = 0; i < atts.getLength(); i++) {
                  AttributeName = atts.getLocalName(i);
                  AttributeType = atts.getType(AttributeName);
                  AttributeValue = atts.getValue(AttributeName);
                  System.out.println ("Attribute: \"" + AttributeName + "\"");
                  System.out.println ("\tType: \"" + AttributeType + "\"");
                  System.out.println ("\tValue: \"" + AttributeValue + "\"");
            }


Hope this helps
0
 
LVL 3

Accepted Solution

by:
applekanna earned 740 total points
ID: 9767515
Hi Asolmo

here is a sample workiing code that process your input file

This is just the XML parsing, I guess from here you can get the picture as how to get the variable into JSPpage, use a servelt to process the XML and then retrun the varaible or use a bean.
If you need more help.. mail again

*******
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import java.io.StringWriter;


public class ParseCurr
{

public static void main(String args[])
        {

if(args.length != 1)
                {
        System.out.println("usage: java parseCurr <fileName>");
                }

try
{
        //make an instance of factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //set it namespace aware
        factory.setNamespaceAware(true);
        //make an isntance of doc builder
        DocumentBuilder builder = factory.newDocumentBuilder();
        //make a document
        Document doc = builder.parse(args[0]);

        //make a node list
        NodeList cubeNL = doc.getElementsByTagName("Cube");
       
        for(int i = 0 ; i< cubeNL.getLength() ; i++)
        {
          Node child = cubeNL.item(i);
          if (child.hasAttributes() ) //if it is true
          {
            System.out.println("hi");
            NamedNodeMap  nnm= child.getAttributes();
            for (int j = 0; j< nnm.getLength(); j++)
            {
              Node node = nnm.item(0);
              System.out.println(" node name = " + node.getNodeName());
              System.out.println(" node name = " + node.getNodeValue());              
            }
          }

        }//end for loop


}
catch (Exception e)
{
        System.out.println(e.getMessage());
}

        }//end of main

}//end of class
***********

Hope this helps
Cheers!
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9767858
Oops Inisde the j loop i have made a mistake

it should be
Node node = nnm.item(j);

and not
Node node = nnm.item(0);

Cheers!
0
 

Author Comment

by:asolmo
ID: 9769892
It works ok!!

Thanks a lot!!

Antonio
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9771787
Thx for the points
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question