[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

eurofxref parse xml using j2ee tecnologies

Posted on 2003-11-17
10
Medium Priority
?
380 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Currently, there is an issue with being able to copy values from an external application to a dropdown list in Project Web Access (PWA).  The standard copy and paste methods don't seem to work properly. Here is a way to accomplish this task to s…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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