从 URL 读取 XML 文件

我尝试从https://www.nbp.pl/kursy/xml/c001z020102.xml读取 xml 文件。


我添加了带有数字的 sout 以观察进度。


public void wypisanie() throws Exception

        {

            URL url = new URL("https://www.nbp.pl/kursy/xml/c001z020102.xml");

            System.out.println("1");

            URLConnection urlConnection = url.openConnection();

            System.out.println("2");

            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            System.out.println("3");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

            System.out.println("4");

            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

            System.out.println("5");

            Document doc1 = dBuilder.parse( in );

            System.out.println("6");

            Element element1 = doc1.getDocumentElement();

            System.out.println("7");

            NodeList nodes1 = element1.getChildNodes();

            System.out.println("8");

            for (int i = 0; i < nodes1.getLength(); i++) {

                System.out.println("" + nodes1.item(i).getTextContent());

            }

        }

这是结果:


1

2

https://www.nbp.pl/kursy/xml/c001z020102

我的目标是从此 URL 获取美元汇率。有人有其他想法我应该怎么做?


编辑:我尝试使用像这样的其他 URL: https ://www.w3schools.com/xml/plant_catalog.xml 并且程序有效。那么“旧”网址有什么问题? https://www.nbp.pl/kursy/xml/c001z020102.xml


“旧”网址的结果


1

2

3

4

5

C:\Users\user\IdeaProjects\NBP\untitled\abch.dtd (The system cannot find the file specified)


慕标琳琳
浏览 88回答 1
1回答

慕尼黑5688855

您的机器上没有 DTD 文件。尝试这个:&nbsp; &nbsp; public void wypisanie() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; URL url = new URL("https://www.nbp.pl/kursy/xml/c001z020102.xml");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("1");&nbsp; &nbsp; &nbsp; &nbsp; URLConnection urlConnection = url.openConnection();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("2");&nbsp; &nbsp; &nbsp; &nbsp; InputStream in = new BufferedInputStream(urlConnection.getInputStream());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("3");&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; dbf.setValidating(false);&nbsp; &nbsp; &nbsp; &nbsp; dbf.setNamespaceAware(true);&nbsp; &nbsp; &nbsp; &nbsp; dbf.setFeature("http://xml.org/sax/features/namespaces", false);&nbsp; &nbsp; &nbsp; &nbsp; dbf.setFeature("http://xml.org/sax/features/validation", false);&nbsp; &nbsp; &nbsp; &nbsp; dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);&nbsp; &nbsp; &nbsp; &nbsp; dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("4");&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder dBuilder = dbf.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("5");&nbsp; &nbsp; &nbsp; &nbsp; Document doc1 = dBuilder.parse(in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("6");&nbsp; &nbsp; &nbsp; &nbsp; Element element1 = doc1.getDocumentElement();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("7");&nbsp; &nbsp; &nbsp; &nbsp; NodeList nodes1 = element1.getChildNodes();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("8");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < nodes1.getLength(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("pozycja".equals(nodes1.item(i).getNodeName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("" + nodes1.item(i).getTextContent());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java