java读取XML文件里面的数据之DOM实现
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
public class ReadXML {
private File file;
public ReadXML(String filename){
File file=new File(filename);
this.file=file;
}
/**
*
* @return the instance of Document
*/
public Document getDOM(){
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document document=null;
try{
db=dbf.newDocumentBuilder();
document=db.parse(file);
}catch(Exception e ){
e.printStackTrace();
}
return document;
}
/**
*
* @param tagName
* @return
*/
public String getInfoByTagName(String tagName)
{
String info="";
Document document=this.getDOM();
//获取NodeName 为tagName的节点组
NodeList nl=document.getElementsByTagName(tagName);
for(int i=0;i<nl.getLength();i++)
{
info+=tagName+"[+"+i+"+]"+"\n";
Node node=nl.item(i);
//如果当前节点有子节点(这里 只考虑还有一级子节点的情况)
if(node.hasChildNodes())
{
NodeList list=node.getChildNodes();
for(int j=0;j<list.getLength();j++)
{
Node childNode=list.item(j);
/* 不加这个If语句会抛出
* Exception in thread "main" java.lang.NullPointerException
* at ReadXML.getInfoByTagName(ReadXML.java:59)
* at Test.main(Test.java:17)
*/
if(childNode.getFirstChild()!=null)
/* 对getNodeValue()的过程彻底无语
* 调试这个地方的时候,在网上很容易找到了
* 在得到Value的时候必须在节点对象后面先调用getFirstChild()或者getChildNodes().item(0)
* 原因很简单,但是不知道设计者问什么要这么设计
* 最近在看《Be
相关文档:
Java学习从入门到精通
一、 JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......
import java.util.Date;
public class TimeSpan
{
public final static TimeSpan ZERO = new TimeSpan(0);
private long _totalMilliSeconds = 0;
public TimeSpan(long totalMilliSeconds)
{
_totalMilliSeconds = totalMilliSeconds;
}
public TimeSpan(Date afterDate, Date beforeDat ......
2009-04-14 15:37
虽然现在用APACHE COMMONS DBCP可以非常方便的建立数据库连接池,
但是像这篇文章把数据库连接池的内部原理写的这么透彻,注视这么完整,
真是非常难得,让开发人员可以更深层次的理解数据库连接池,真是非常感
谢这篇文章的作者。
import java.sql.Connection;
import java.sql.DatabaseMetaData;
......
import java.io.*;
class BigInt
{
int a[];
int len;
BigInt(String str)
{
{
len=str.length();
a=new int[len];
for(int i=0;i<len;i++)
{
this.a[i]=str.charAt(i)-48;
}
&nb ......
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.huadoo.model.Area;
public class Aaaa {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
  ......