I try to read xml file from https://www.nbp.pl/kursy/xml/c001z020102.xml .
I added sout's with numbers to observe progress.
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());
}
}
This is the result:
1
2
https://www.nbp.pl/kursy/xml/c001z020102
My target is getting USD rate from this URL. Someone have other ideas how I should do it?
Edit: I try use other URL like this: https://www.w3schools.com/xml/plant_catalog.xml and program works. So what is wrong with "old" url? https://www.nbp.pl/kursy/xml/c001z020102.xml
Result of "old" url
1
2
3
4
5
C:\Users\user\IdeaProjects\NBP\untitled\abch.dtd (The system cannot find the file specified)
You don't have DTD file on your machine.
Try this:
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 dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setNamespaceAware(true);
dbf.setFeature("http://xml.org/sax/features/namespaces", false);
dbf.setFeature("http://xml.org/sax/features/validation", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
System.out.println("4");
DocumentBuilder dBuilder = dbf.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++) {
if ("pozycja".equals(nodes1.item(i).getNodeName())) {
System.out.println("" + nodes1.item(i).getTextContent());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.