继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

xml 解析(DOM)视频总结

捡肥皂的男孩
关注TA
已关注
手记 3
粉丝 5
获赞 161

DOM解析步骤
1.创建documentBuilderFactory
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
2.documentBuilder
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
3.解析xml文本 documentBuilder.parse(file)

方法
1.先获取子节点,Node下有方法getChildNodes()来获取某个节点的子节点的集合,返回NodeList类型.
NodeList childNodes=book.getChildNodes()//包含book节点所有的子节点,两个标签之间的所有内容都看成是子节点.
2.通过childNodes的getLength()方法返回字点的个数(空格与换行字符看成为一个文本节点,标签与结束标签看成一个元素节点)
3.通过NodeList的item(i)获取指定位置子节点的名称返回Node类型.再用Node类型的getNodeName()方法就可以获取节点名
Node childnode=childNodes.item(i);
String name=childnode.getNodeName();
可以通过Node类的getNodeType()来区分文本类型的node以及元素类型的node,看当前Node类型是否与Node."节点类型英文全称"相同.
if(childnode.getNodeType==Node.ELEMENT_NODE)
{System.out.println(name)}
4.不能直接通过Node的getNodeValue()来获取节点的值,因为元素节点的nodeValue的返回值为null而且标签之间的文本被看做是该标签的子节点.所以要用Node的getFirstChild()此时获取的子节点为文本节点,Text节点类型的nodeValue返回值为节点内容,再getNodeValue()。
或者直接用Node的getTextContent()方法直接获取节点值。

简单demo

public class DomAnalyDemo {

    public static void systemXml(Document document,String rootNode) throws ParserConfigurationException, IOException, SAXException {

        NodeList nodeList = document.getElementsByTagName(rootNode);
        // 遍历每个content
//        System.out.println(nodeList.getLength());
        for (int i = 0; i <nodeList.getLength() ; i++) {
            Node node = nodeList.item(i);

            // 得到子节点列表
            NodeList childChildList = node.getChildNodes();

            // 遍历子节点列表
            for (int j = 0; j < childChildList.getLength(); j++) {

                // 取得子节点的的第 index 个项。
                Node childNode = childChildList.item(j);
                // 子节点类型为text
//                if(childNode.getNodeType() == Node.TEXT_NODE){
//                    System.out.println("子节点类型为text");
//                }
                //判断是否是 text,如果节点类型为ele类型
                if(childNode.getNodeType() == Node.ELEMENT_NODE){
                    System.out.print("<"+childNode.getNodeName()+">");

                    // 取得孙节点
                    NodeList sunNodeList = childNode.getChildNodes();

                    // 如果孙节点长度 >1  递归
                    if(sunNodeList.getLength() <= 1){
                        // 取出子节点内容
                        System.out.print(childNode.getTextContent());
                    }else {
                        SystemXml(document,childNode.getNodeName());
                    }
                    System.out.println("</"+childNode.getNodeName()+">");

                }
            }
            System.out.println("");

        }

//        System.out.println("<"+ rootNode+"/>");

    }

    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {

        File file = new File("F:\\springBootDemo\\activityDemo\\src\\test\\java\\xmlanalysis\\xmlDemo.xml");
        if(!file.exists()){
            System.out.println("xml文件不存在");
            return;
        }else {
            // 创建documentBuilderFactory工厂类
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            // 解析xml文本
            Document document = documentBuilder.parse(file);
            systemXml(document,"content");
        }
    }
}

输出结果

<bizUserInfoImportInfoList><id>编号</id>
<userNo>业务账号</userNo>
<userType>用户类型</userType>
<entNo>企业编号</entNo>
<accountType>账号类型</accountType>
<bizNo>业务编号</bizNo>
<tcid>套餐编号</tcid>
<phone>联系电话</phone>
<intime>创建时间</intime>
<insource>注册来源</insource>
<remark>备注</remark>
<insourceName>开通名称</insourceName>
<taocanName>套餐名称</taocanName>

</bizUserInfoImportInfoList>
<pageinfo><showPageSize>每页记录数</showPageSize>
<offset>总页数</offset>
<limit>每页限制记录数</limit>
<totalcount>总记录数</totalcount>
<totalpage>总页数</totalpage>
<pagesize>每页记录数</pagesize>
<currentpage>当前页码</currentpage>

</pageinfo>

使用dom 简单读取xml

打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP