0

I need parsing xml from url url xml

<?xml version="1.0" encoding="UTF-8"?>
<exchangerates>
  <row>
    <exchangerate ccy="RUR" base_ccy="UAH" buy="0.33291" sale="0.33291"/>
  </row>
  <row>
    <exchangerate ccy="EUR" base_ccy="UAH" buy="18.60253" sale="18.60253"/>
  </row>
  <row>
    <exchangerate ccy="USD" base_ccy="UAH" buy="14.97306" sale="14.97306"/>
  </row>
</exchangerates>

I whant get attribute for using "14.97306" to convert my currency

(like mycurrency = 10 )
(like usd = 14.97306 )
(mycurrency * usd = 149.7306)
4
  • What programming language do you plan to use for it? Commented Nov 27, 2014 at 8:57
  • javascript, it`s will be web page. Commented Nov 27, 2014 at 9:04
  • 1
    Than you should remove JAVA tag :) The answer bellow from geert3 is for JAVA Commented Nov 27, 2014 at 9:07
  • @AndreyE - and the subject title Commented Nov 27, 2014 at 9:10

1 Answer 1

1

Here's a rudimentary setup for Java. You'll need to read up on XPATH queries, and you need to add exception handling etc. But I'm sure this will get you started.

    // open the URL
    URL url = ....
    InputStream is = url.openStream();

    // build a document parser
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();

    // parse the document from the URL
    Document d = builder.parse(is);

    // set up XPATH to examine/query the XML
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    // use XPATH query to find the required information
    String sale = xpath.evaluate("/exchangerates/row[@ccy='USD']/@sale", d);

    System.out.println("USD sale is "+sale);
Sign up to request clarification or add additional context in comments.

3 Comments

where this code must parsing info, I have white page =( I know java very bad sorry
Sorry chap. I can offer some food but chewing and swallowing is up to you.
that's the spirit ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.