Java 怎么从URI读取XML文档(Document)

2019年7月28日22:05:46Java 怎么从URI读取XML文档(Document)已关闭评论 679 827字阅读2分45秒

1.简介

Java 怎么从URI读取XML文档(Document)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 

import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.*;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSParser;

public class Main {
    public static Document readDocument(String uri) {
        Document document = null;
        try {
            DOMImplementationRegistry registry = DOMImplementationRegistry
                    .newInstance();

            DOMImplementationLS impl = (DOMImplementationLS) registry
                    .getDOMImplementation("LS");

            LSParser builder = impl.createLSParser(
                    DOMImplementationLS.MODE_SYNCHRONOUS, null);

            System.out.println("In readDocument uri: " + uri);
            document = builder.parseURI(uri);
            if (document == null) {
                System.out.println("Null doc returned!!!!!!!!");
            }
            System.out.println("Read toc: \n " + document);
        } catch (Exception e) {
            System.out.println("ERROR reading toc: \n ");
            e.printStackTrace();
        }
        return document;
    }
}

 
继续阅读
编程十万个怎么办