eurofxref parse xml using j2ee tecnologies


I have wasted many hours and days now trying to figure out how to grab the currency rates off of ECB's xml-page:

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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hello asolmo,


asolmoAuthor Commented:
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?

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

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.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

asolmoAuthor Commented:
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

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.
This link has something that you are asking for

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

public class ParseCurr

public static void main(String args[])

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

        //make an instance of factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //set it namespace aware
        //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
            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)

        }//end of main

}//end of class

Hope this helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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);

asolmoAuthor Commented:
It works ok!!

Thanks a lot!!

Thx for the points
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.