App下載
首頁(yè)javadomJava HTML/XML - 如何從URL讀取XML文檔

Java HTML/XML - 如何從URL讀取XML文檔

我們想知道如何從URL讀取XML文檔。
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class Main {

    private static Document loadTestDocument(String url) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(new URL(url).openStream());
    }

    public static void main(String[] args) throws Exception {
        Document  doc = loadTestDocument("http://localhost/array.xml");
        System.out.println(doc);
    }
}