package com.converter;

import java.io.IOException;
import java.net.URI;
import java.net.URL;

import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXParseException;
//import XmlReader.java;
public class XMLReader {
 public  Float value = 25f;

public XMLReader(){
 String parseString = "";


 try {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  //  dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    URI uri = new URI("http://themoneyconverter.com/USD/rss.xml");

    ****Document doc = db.parse(uri.toString());****

    doc.getDocumentElement().normalize();
   NodeList nodeLst = doc.getElementsByTagName("description");
   int length = nodeLst.getLength();

    for (int s = 0; s < length; s++) {
     Node fstNode = nodeLst.item(s);
     parseString = fstNode.getTextContent();

     if(parseString.contains("Indian Rupee")){
      System.out.println(parseString);
      StringTokenizer parser = new StringTokenizer(parseString,"=");
      parser.nextToken();
      StringTokenizer parser1 = new StringTokenizer(parser.nextToken());
      value = Float.valueOf(parser1.nextToken());
      System.out.println(value);

     }

    }
    } catch (SAXParseException e) {
     value = 30f;
      e.printStackTrace();
    }catch (IOException e) {
     value = 33f;
       e.printStackTrace();
   }catch (Exception e) {
      value = 32f;
      e.printStackTrace();
   }

}

}

最佳答案

这是我的输出工作:

1 US Dollar = 45.92697 Indian Rupee
45.92697


因此,我想您在访问资源时遇到了一些网络问题。检查防火墙设置,防病毒程序等。

如果您需要更多帮助,也可以在此处粘贴堆栈跟踪信息;)

干杯!

关于java - DocumentBuilder.parse(String Uri)返回IOException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4306981/

10-12 22:20