Java调用XML的方法:DocumentBuilderFactory

    xiaoxiao2021-03-25  124

    首先得到:得到 DOM 解析器的工厂实例   

      DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();

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

     DocumentBuilder dombuilder=domfac.newDocumentBuilder();

     

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

    File xmlFile = new File("d:\\test1.xml"); 

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

     Document doc=dombuilder.parse(xmlFile );

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

    Element root=doc.getDocumentElement();

    ( 6 )得到节点的子节点

      NodeList books=root.getChildNodes();

     

    Java代码  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();                 File xmlFile = new File("d:\\test1.xml");        Document doc = domBuilder.parse(xmlFile);               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.getFirstChild().getNodeValue();                                            System.out.println(name);                                        }                                        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>gsf</name>           <price>445</price>       </book>   </books>  另一种实现方式: public Map<String, Double> getFormulaList(String pathA) { // TODO Auto-generated method stub List<List> formulaList = new ArrayList<List>(); // 创建解析的xml文档对象, File xmlFile = new File(pathA); // 声明一个 DocumentBuilder对象. 抽象类,不能直接构建,可以通过 DocumentFactory 来构建。 DocumentBuilder builder = null; // 声明一个 DocumentBuilderFactory对象. 通过单例模式创建 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); Map<String, Double>results=new HashMap<String, Double>(); // 取得默认的 DocumentBuilder. try { builder = builderFactory.newDocumentBuilder(); // 解析文件 Document document = builder.parse(xmlFile); // 获得根元素 Element root = document.getDocumentElement(); System.out.println("根元素:" + root.getNodeName()); // 获得根元素下的子节点 // NodeList childNodes = root.getChildNodes(); NodeList childNodes=root.getElementsByTagName("Formula"); // 遍历这些子节点 for (int i = 0; i < childNodes.getLength(); i++) { String key=root.getElementsByTagName("name").item(i).getFirstChild().getNodeValue(); Double value=Double.parseDouble(root.getElementsByTagName("percent").item(i).getFirstChild().getNodeValue()); results.put(key, value); } }catch (Exception e) { e.printStackTrace(); } return results; } xml代码: <?xml version="1.0" encoding="UTF-8"?> <FormulaList> <Formula> <name>完成度</name> <percent>0.4</percent> </Formula> <Formula> <name>知晓度</name> <percent>0.2</percent> </Formula> <Formula> <name>参与度</name> <percent>0.2</percent> </Formula> <Formula> <name>满意度</name> <percent>0.2</percent> </Formula> </FormulaList>
    转载请注明原文地址: https://ju.6miu.com/read-11474.html

    最新回复(0)