加载xml的过程

//资源路径
String location = "";
//通过路径加载到资源
URL resource = this.getClass().getClassLoader().getResource(location);
//
URL url = resource ;
//获得资源的连接
URLConnection urlConnection = url.openConnection();
//打开链接
 urlConnection.connect();
//获得文件的input流
        return urlConnection.getInputStream();
//从xml文件解析到DOM文件
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(inputStream);
//解析Document文件
 Element root ;
解析为Element
 root = doc.getDocumentElement();
//获取元素的子节点
NodeList nl = root.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element) {
                Element ele = (Element) node;
                                //获取到节点的内容
                                //就可以对内容做操作了.
                String name = ele.getAttribute("name");
                        String className = ele.getAttribute("class");
            }
        }

你可能感兴趣的:(加载xml的过程)