XML 实用工具类
发布: 2008-6-25 20:27 | 作者: admin | 查看: 12次
XML 实用工具类
JSP教程-Java与XML
package com.elink.util;
/*
*
Company: 凌科软件 www.elingke.com
* @author liubaojun
* @version 1.0
* Created on 2004-11-29
* 来源于 elinkBSP 部分源代码
*/
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class XmlUtil
{
public static synchronized Document newDocument()
{
Document doc = null;
try
{
DocumentBuilder db = DocumentBuilderFactory.newInstance().
newDocumentBuilder();
doc = db.newDocument();
}
catch (Exception e)
{
LogUtil.logException( e );
}
return doc;
}
public static synchronized Element createRootElement()
{
Element rootElement = null;
try
{
DocumentBuilder db = DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Document doc = db.newDocument();
rootElement = doc.getDocumentElement();
}
catch (Exception e)
{
e.printStackTrace();
}
return rootElement;
}
public static synchronized Element getRootElement(String fileName)
{
if (fileName == null || fileName.length() == 0)
{
return null;
}
try
{
Element rootElement = null;
FileInputStream fis = new FileInputStream(fileName);
rootElement = getRootElement(fis);
fis.close();
return rootElement;
}
catch (Exception e)
{
return null;
}
}
public static synchronized Element getRootElement(InputStream is)
{
if (is == null)
{
return null;
}
Element rootElement = null;
try
{
DocumentBuilder db = DocumentBuilderFactory.newInstance().
newDocumentBuilder();
Document doc = db.parse(is);
rootElement = doc.getDocumentElement();
}
catch (Exception e)
{
e.printStackTrace();
}
return rootElement;
}
public static synchronized Element getRootElement(InputSource is)
{
if (is == null)
{
return null;
}
Element rootElement = null;












