解析xml文件实例

 

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.math.BigDecimal;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * <b>功能描述:解析xml文件</b><br/>
 * <p>
 * 创建时间: 2007-07-01 <br/>
 * 
 * 
@version 1.00
 * 
@author yefeng
*/

public class TestXML {
 
private void getXMLFile(String fileName) throws Exception {
 
//创建 DocumentBuilderFactory。 DocumentBuilderFactory 对象创建 DocumentBuilder。 
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  
//创建 DocumentBuilder。 DocumentBuilder 执行实际的解析以创建 Document 对象。
  DocumentBuilder builder = factory.newDocumentBuilder();
  
//解析文件以创建 Document 对象。 
  Document doc = builder.parse(new File(fileName));

  Node rootNode 
= getChildNodeByName(doc, "departFile");
  Node summaryNode 
= getChildNodeByName(rootNode, "TotalField");

  
// 解析xml文件
  NamedNodeMap attr = summaryNode.getAttributes();
  String SumDepart 
= attr.getNamedItem("SumDepart").getNodeValue();
  String TotalCount 
= attr.getNamedItem("TotalCount").getNodeValue();
  printXML(SumDepart, TotalCount);
 }


 
private Node getChildNodeByName(Node parent, String name) throws Exception {
  NodeList nList 
= parent.getChildNodes();
  
for (int i = 0; i < nList.getLength(); i++{
   Node tmp 
= nList.item(i);
   
if (name.equals(tmp.getNodeName())) {
    
return tmp;
   }

  }

  
return null;
 }

 
private void getDetailXML(String XMLFile) throws Exception {
  BufferedReader in 
= null;
  String aFileName 
= XMLFile;
  
try {
   in 
= new BufferedReader(new FileReader(aFileName));
   String line;
   
// 解析批量文件中的有效数据(文件中包含的每笔指令)
   while (true{
    line 
= in.readLine();
    
if (line == null)
     
break;
    
if (line.trim().length() == 0)
     
continue;
    
// 表示文件结束
    if (line.trim().indexOf("</departFile"!= -1)
     
break;
    
// 过滤无效行
    if (line.trim().length() == 0 || line.indexOf("<!"!= -1
      
|| line.indexOf("<Record "== -1)
     
continue;

    
// 用xml来解析明细
    DocumentBuilderFactory factory = DocumentBuilderFactory
      .newInstance();
    DocumentBuilder builder 
= factory.newDocumentBuilder();
    ByteArrayInputStream stream 
= new ByteArrayInputStream(line
      .getBytes(
"UTF-8"));
    Document doc 
= builder.parse(stream);
    Node record 
= doc.getFirstChild();
    NamedNodeMap attr 
= record.getAttributes();
    
for (int i = 0; i < attr.getLength(); i++{
     String name 
= attr.item(i).getNodeName();
     String value 
= attr.item(i).getNodeValue();
     
//打印信息
     System.out.print(value+" ");
    }

    System.out.println(
"");
   }

  }
 catch (Exception e) {
   System.out.println(e.getMessage());
  }
 finally {
   
if (in != null{
    in.close();
   }

  }


 }

 
 
private void printXML(String str1, String str2) {//打印概要信息
  System.out.println("SumDepart: " + str1);
  System.out.println(
"TotalCount: " + str2);
 }


 
public static void main(String[] str) {
  TestXML xmlObj 
= new TestXML();
  String fileName 
= "XMLFile.xml";
  
try {
   xmlObj.getXMLFile(fileName);
   System.out.println(
"departName"+" "+"departNo"+" "+"count"+"  "+"remark");
   xmlObj.getDetailXML(fileName);
  }
 catch (Exception e) {
   System.out.println(e.getMessage());
  }

 }

}
 

 

/*

xml格式

*/

<?xml version="1.0" encoding="GB2312"?>
<departFile>
 
<TotalField SumDepart="3" TotalCount="300"/>
 
<!--#部门名称|部门id|部门人数|摘要|-->
 
<RecordList>
  
<Record departName="depart1" departNo="1" count="120" remark="sd"/>
  
<Record departName="depart2" departNo="2" count="100" remark="dd"/>
  
<Record departName="depart3" departNo="3" count="80" remark="aaaa"/>
 
</RecordList>
</departFile>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值