Solved

eurofxref parse xml using j2ee tecnologies

Posted on 2003-11-17
10
362 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
password protect pdf 11 76
love6 challenge java 31 133
difference between projection and projection list in hibernate 1 110
how to hide hide popup page refresh second time? 10 126
This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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