Solved

eurofxref parse xml using j2ee tecnologies

Posted on 2003-11-17
10
358 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 185 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
decryption not working 5 80
xjc and jaxb 2 88
love6 challenge java 31 128
Suggestion on WebSite Template Sites 6 55
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now