На самом деле ваш XML-документ был проанализирован и загружен правильно.
Вы были только раздражены довольно глупым выводом doc.toString()
(который вызывается за кадром при оценке "XML " + doc
).
Предварительно вы знаете ожидаемые имена тегов XML (urlset
, url
, loc
, lastmod
)
и как они вложены друг в друга.
![XML structure](https://i.stack.imgur.com/ehos7.png)
С этим знанием вам просто нужно продолжать ходить по дереву XML
и извлекать то, что вы хотите. Например, как это:
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = factory.newDocumentBuilder().parse(new URL("https://www.lavisducagou.nc/page-sitemap.xml").openStream());
// Get the <urlset> root element
Element urlsetElement = doc.getDocumentElement();
// Get the list of <url> elements within the <urlset> element
NodeList urlNodeList = urlsetElement.getElementsByTagName("url");
for(int i = 0; i < urlNodeList.getLength(); i++) {
// Get the <url> element
Element urlElement = (Element) urlNodeList.item(i);
// Get the <loc> element within the <url> element
Element locElement = (Element) urlElement.getElementsByTagName("loc").item(0);
// Print the text content of the <lo> element
System.out.println("loc = " + locElement.getTextContent());
// Get the <lastmod> element within the <url> element
Element lastmodElement = (Element) urlElement.getElementsByTagName("lastmod").item(0);
// Print the text content of the <lastmod> element
System.out.println("lastmod = " + lastmodElement.getTextContent());
}
}
Вы получите вывод, подобный следующему:
loc = https://www.lavisducagou.nc/
lastmod = 2018-07-14T11:30:25+11:00
loc = https://www.lavisducagou.nc/sinscrire/
lastmod = 2018-05-03T16:58:35+11:00
loc = https://www.lavisducagou.nc/se-connecter/
lastmod = 2018-05-03T18:02:07+11:00
loc = https://www.lavisducagou.nc/mot-de-passe-oublie/
lastmod = 2018-05-03T20:33:08+11:00
loc = https://www.lavisducagou.nc/compte/
lastmod = 2018-05-03T20:36:32+11:00
...