Java调用XML的方法:DocumentBuilderFactory

首先得到:得到 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代码  复制代码
  1. package com.st.demo;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.InputStream;   
  6.   
  7. import javax.xml.parsers.DocumentBuilder;   
  8. import javax.xml.parsers.DocumentBuilderFactory;   
  9.   
  10. import org.w3c.dom.Document;   
  11. import org.w3c.dom.Element;   
  12. import org.w3c.dom.Node;   
  13. import org.w3c.dom.NodeList;   
  14.   
  15. public class XmlReader {   
  16.     public static void main(String[] args) {   
  17.         XmlReader reader = new XmlReader();   
  18.     }   
  19.     public XmlReader(){   
  20.         DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();   
  21.         try {   
  22.             DocumentBuilder domBuilder = domfac.newDocumentBuilder();   
  23.              File xmlFile = new File("d:\\test1.xml");        Document doc = domBuilder.parse(xmlFile);
  24.  
  25.             Element root = doc.getDocumentElement();   
  26.             NodeList books = root.getChildNodes();   
  27.             if(books!=null){   
  28.                 for (int i = 0; i < books.getLength(); i++) {   
  29.                     Node book = books.item(i);   
  30.                      if(book.getNodeType()==Node.ELEMENT_NODE) {   
  31.                             //(7)取得节点的属性值   
  32.                             String email=book.getAttributes().getNamedItem("email").getNodeValue();   
  33.                             System.out.println(email);   
  34.                             //注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE   
  35.                             //(8)轮循子节点   
  36.                             for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {   
  37.                                 if(node.getNodeType()==Node.ELEMENT_NODE) {   
  38.                                     if(node.getNodeName().equals("name")) {                                                        String name=node.getFirstChild().getNodeValue();   
  39.                                         System.out.println(name);   
  40.                                     }   
  41.                                     if(node.getNodeName().equals("price")) {   
  42.                                         String price=node.getFirstChild().getNodeValue();  
  43.                                         System.out.println(price);   
  44.                                     }   
  45.                                 }   
  46.                             }   
  47.                      }   
  48.                 }   
  49.             }   
  50.         } catch (Exception e) {   
  51.             // TODO Auto-generated catch block   
  52.             e.printStackTrace();   
  53.         }   
  54.            
  55.     }   


Xml代码  复制代码
  1. <?xml version="1.0" encoding="GB2312" standalone="no"?>  
  2. <books>  
  3.     <book email="zhoujunhui">  
  4.         <name>gsf</name>  
  5.         <price>445</price>  
  6.     </book>  
  7. </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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值