Solved

eurofxref parse xml using j2ee tecnologies

Posted on 2003-11-17
10
367 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java cyclic graphs 15 223
eclipse luna javeEE perspecive missing 5 122
Java Script nested call 3 91
maven not picking latest jar instead picking old jar from .m2 12 74
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
Invest in your employees with these five simple steps to improve employee engagement and retention.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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