zoukankan      html  css  js  c++  java
  • Java调用XML的方法:DocumentBuilderFactory

    首先得到:得到 DOM 解析器的工厂实例      DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();

    然后从 DOM 工厂获得 DOM 解析器

     DocumentBuilder dombuilder=domfac.newDocumentBuilder();

    3 )把要解析的 XML 文档转化为输入流,以便 DOM 解析器解析它

    InputStream is= new  FileInputStream("test1.xml");        

    ( 4 )解析 XML 文档的输入流,得到一个 Document

     Document doc=dombuilder.parse(is);

    ( 5 )得到 XML 文档的根节点

    Element root=doc.getDocumentElement();

    ( 6 )得到节点的子节点

      NodeList books=root.getChildNodes();

    package com.st.demo;   
      
    import java.io.File;   
    import java.io.FileInputStream;   
    import java.io.InputStream;   
      
    import javax.xml.parsers.DocumentBuilder;   
    import javax.xml.parsers.DocumentBuilderFactory;   
      
    import org.w3c.dom.Document;   
    import org.w3c.dom.Element;   
    import org.w3c.dom.Node;   
    import org.w3c.dom.NodeList;   
      
    public class XmlReader {   
        public static void main(String[] args) {   
            XmlReader reader = new XmlReader();   
        }   
        public XmlReader(){   
            DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();   
            try {   
                DocumentBuilder domBuilder = domfac.newDocumentBuilder();   
                InputStream is = new FileInputStream(new File("D:/test1.xml"));   
                Document doc = domBuilder.parse(is);   
                Element root = doc.getDocumentElement();   
                NodeList books = root.getChildNodes();   
                if(books!=null){   
                    for (int i = 0; i < books.getLength(); i++) {   
                        Node book = books.item(i);   
                         if(book.getNodeType()==Node.ELEMENT_NODE) {   
                                //(7)取得节点的属性值   
                                String email=book.getAttributes().getNamedItem("email").getNodeValue();   
                                System.out.println(email);   
                                //注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE   
                                //(8)轮循子节点   
                                for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {   
                                    if(node.getNodeType()==Node.ELEMENT_NODE) {   
                                        if(node.getNodeName().equals("name")) {   
                                            String name=node.getNodeValue();   
                                            String name1=node.getFirstChild().getNodeValue();   
                                            System.out.println(name);   
                                            System.out.println(name1);   
                                        }   
                                        if(node.getNodeName().equals("price")) {   
                                            String price=node.getFirstChild().getNodeValue();   
                                            System.out.println(price);   
                                        }   
                                    }   
                                }   
                         }   
                    }   
                }   
            } catch (Exception e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
               
        }   
    }  
    

      

    XML:

    <?xml version="1.0" encoding="GB2312" standalone="no"?>  
    <books>  
        <book email="zhoujunhui">  
            <name>rjzjh</name>  
            <price>jjjjjj</price>  
        </book>  
    </books>  
  • 相关阅读:
    判断变量是否是数组
    手机闹钟功能: 使用状态图解决闹钟响铃问题
    logging基本使用方法
    ssh 通过跳板机连接到远程服务器
    ssh 执行命令并实时显示结果
    使用 python 将 " " 转换为 " "
    python 文件操作
    python SMTP 发送邮件
    C++统一初始化
    设计模式之单例模式实现(C++)
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/7918886.html
Copyright © 2011-2022 走看看