3 回答

TA貢獻1784條經(jīng)驗 獲得超7個贊
您正在查看的只是 toString 的實現(xiàn) com.sun.org.apache.xerces.internal.dom.DocumentImpl
public String toString() {
return "["+getNodeName()+": "+getNodeValue()+"]";
}
由于文檔沒有節(jié)點值,因此它為空。您需要做的是獲取 childNodes 并迭代并獲取所需的詳細信息。
由于防火墻問題,我無法使用 java 訪問 URL,但這里有來自同一文件本身的一小段摘錄。
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//www.lavisducagou.nc/wp-content/plugins/wordpress-seo/css/main-sitemap.xsl"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.lavisducagou.nc/</loc>
<lastmod>2018-07-14T11:30:25+11:00</lastmod>
</url>
<url>
<loc>https://www.lavisducagou.nc/sinscrire/</loc>
<lastmod>2018-05-03T16:58:35+11:00</lastmod>
</url>
</urlset>
剛剛通過以下步驟更新了您的代碼:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(new URL("https://www.lavisducagou.nc/page-sitemap.xml").openStream());
System.out.println("XML = " + doc);
NodeList nodeList = doc.getChildNodes();
for (int i=0; i<nodeList.getLength();i++) {
System.out.println(nodeList.item(i).getNodeName());
}
示例輸出:
XML = [#document: null]
xml-stylesheet
urlset
添加回答
舉報