Java 怎么将XML从inputstream解析为dom文档

2019年7月29日19:39:02Java 怎么将XML从inputstream解析为dom文档已关闭评论 340 622字阅读2分4秒

1.简介

Java 怎么将XML从inputstream解析为dom文档?——请看下文。

2.示例代码

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


import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class Main {
    public static Document parseXML(InputStream stream) throws Exception {
        DocumentBuilderFactory objDocumentBuilderFactory = null;
        DocumentBuilder objDocumentBuilder = null;
        Document doc = null;
        try {
            objDocumentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            objDocumentBuilder = objDocumentBuilderFactory
                    .newDocumentBuilder();

            doc = objDocumentBuilder.parse(stream);
        } catch (Exception ex) {
            throw ex;
        }

        return doc;
    }
}

 
编程十万个怎么办