首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 图形图象-> Maya教程
XML 实用工具类-JSP教程,Java与XML
作者:网友供稿 点击:236
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
package com.elink.util;

/*
* <p>company: 凌科软件 www.elingke.com </p>
* @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;
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[] getchildelements(element element)
{
if (element == null)
{
return null;
}
vector childs = new vector();
for (node node = element.getfirstchild(); node != null;
node = node.getnextsibling())
{
if (node instanceof element)
{
childs.add( (element)node);
}
}
element[] elmt = new element[childs.size()];
childs.toarray(elmt);
return elmt;
}

public static synchronized element[] getchildelements(element element,
string childname)
{
if (element == null || childname == null || childname.length() == 0)
{
return null;
}
vector childs = new vector();
for (node node = element.getfirstchild(); node != null;
node = node.getnextsibling())
{
if (node instanceof element)
{
if (node.getnodename().equals(childname))
{
childs.add( (element)node);
}
}
}
element[] elmt = new element[childs.size()];
childs.toarray(elmt);
return elmt;
}

public static synchronized node[] getchildnodes(node node)
{
if (node == null)
{
return null;
}
vector childs = new vector();
for (node n = node.getfirstchild(); n != null;
n = n.getnextsibling())
{
childs.add( (element)n);
}
node[] childnodes = new element[childs.size()];
childs.toarray(childnodes);
return childnodes;
}

public static synchronized element getchildelement(element element,
string childname)
{
if (element == null || childname == null || childname.length() == 0)
{
return null;
}
element childs = null;
for (node node = element.getfirstchild(); node != null;
node = node.getnextsibling())
{
if (node instanceof element)
{
if (node.getnodename().equals(childname))
{
childs = (element)node;
break;
}
}
}
return childs;
}

public static synchronized element getchildelement(element element)
{
if (element == null)
{
return null;
}
element childs = null;
for (node node = element.getfirstchild(); node != null;
node = node.getnextsibling())
{
if (node instanceof element)
{
childs = (element)node;
break;
}
}
return childs;
}

public static synchronized string[] getelenentvalues(element element)
{
if (element == null)
{
return null;
}
vector childs = new vector();
for (node node = element.getfirstchild(); node != null;
node = node.getnextsibling())
{
if (node instanceof text)
{
childs.add(node.getnodevalue());
}
}
string[] values = new string[childs.size()];
childs.toarray(values);
return values;
}

public static synchronized string getelenentvalue(element element)
{
if (element == null)
{
return null;
}
string retnstr = null;
for (node node = element.getfirstchild(); node != null;
node = node.getnextsibling())
{
if (node instanceof text)
{
string str = node.getnodevalue();
if (str == null || str.length() == 0
|| str.trim().length() == 0)
{
continue;
}
else
{
retnstr = str;
break;
}
}
}
return retnstr;
}

public static synchronized element findelementbyname(element e, string name)
{
if (e == null || name == null || name.length() == 0)
{
return null;
}
string nodename = null;
element[] childs = getchildelements(e);
for (int i = 0; i < childs.length; i++)
{
nodename = childs[i].getnodename();
if (name.equals(nodename))
{
return childs[i];
}
}
for (int i = 0; i < childs.length; i++)
{
element retn = findelementbyname(childs[i], name);
if (retn != null)
{
return retn;
}
}
return null;
}
public static synchronized element findelementbyattr(element e, string attrname,
string attrval)
{
return findelementbyattr( e, attrname, attrval, true );
}

public static synchronized element findelementbyattr(element e, string attrname,
string attrval, boolean dept)
{
if (e == null || attrname == null || attrname.length() == 0
|| attrval == null || attrval.length() == 0)
{
return null;
}
string tmpvalue = null;
element[] childs = getchildelements(e);
for (int i = 0; i < childs.length; i++)
{
tmpvalue = childs[i].getattribute(attrname);
if (attrval.equals(tmpvalue))
{
return childs[i];
}
}
if( dept )
{
for (int i = 0; i < childs.length; i++)
{
element retn = findelementbyattr(childs[i], attrname, attrval);
if (retn != null)
{
return retn;
}
}
}
return null;
}

public static synchronized string formatxml(element e)
{
return formatxml(e, 0);
}

/**
* 格式化xml输出串.
*/
public static synchronized string formatxml(element e, int indent)
{
indent++;
for (node n = e.getfirstchild(); n != null; n = n.getnextsibling())
{
appendindent(e, n, indent);
if (!n.getnodename().equals("#text"))
{
formatxml( (element)n, indent);
}
}
indent--;
appendindent(e, indent);
return e.tostring();
}

/**
* 在指定的节点前插入格式表示.
*/
private static synchronized void appendindent(element e, node pos, int indent)
{
document doc = e.getownerdocument();
if (indent == 0)
{
e.insertbefore(doc.createtextnode("\n"), pos);
}
for (int i = 0; i < indent; i++)
{
if (i == 0)
{
e.insertbefore(doc.createtextnode("\n\t"), pos);
}
else
{
e.insertbefore(doc.createtextnode("\t"), pos);
}
}
}

/**
* 追加格式表示.
*/
private static synchronized void appendindent(element e, int indent)
{
document doc = e.getownerdocument();
if (indent == 0)
{
e.appendchild(doc.createtextnode("\n"));
}
for (int i = 0; i < indent; i++)
{
if (i == 0)
{
e.appendchild(doc.createtextnode("\n\t"));
}
else
{
e.appendchild(doc.createtextnode("\t"));
}
}
}

public static synchronized void setattribute(element e, string name, string value)
{
if (e == null || name == null || name.length() == 0 || value == null
|| value.length() == 0)
return;
else
e.setattribute( name, value );
}

public static synchronized string getattribute(element e, string name)
{
return getattribute( e, name, null );
}
public static synchronized string getattribute(element e, string name, string defval)
{
if( e == null || name == null || name.length()== 0 )
return defval;
else
return e.getattribute(name);
}

public void transformerwrite( element doc, string filename ) throws exception
{
domsource doms = new domsource( doc );
file f = new file( filename );
streamresult sr = new streamresult( f );
transformerwrite( doms, sr );
}

public void transformerwrite( element doc, file file ) throws exception
{
domsource doms = new domsource( doc );
streamresult sr = new streamresult( file );
transformerwrite( doms, sr );
}

public void transformerwrite( element doc, outputstream outstream ) throws exception
{
domsource doms = new domsource( doc );
streamresult sr = new streamresult( outstream );
transformerwrite( doms, sr );
}

public void transformerwrite( element doc, writer outwriter ) throws exception
{
domsource doms = new domsource( doc );
streamresult sr = new streamresult( outwriter );
transformerwrite( doms, sr );
}

public void transformerwrite( domsource doms, streamresult sr ) throws exception
{
transformerfactory tf = transformerfactory.newinstance();
transformer t = tf.newtransformer();
t.setoutputproperty( outputkeys.encoding, "gbk" );
t.transform( doms, sr );
}
}



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·C# 1.x 实现 "强类型元素唯一的 ArrayList"-.NET教程,C#语言
·NET(C#)连接各类数据库-集锦-.NET教程,C#语言
·快速測試 Tomcat + Oracle 9i + JSP-JSP教程,Jsp/Servlet
·使用.NET读取XML文件-.NET教程,XML应用
·XML 实用工具类-JSP教程,Java与XML
·如何把ASP编写成DLL-ASP教程,ASP应用
·vb.net中不需要EXCEL导出成XSL-.NET教程,VB.Net语言
·Asp之Request对象(1)-ASP教程,ASP应用
·C#读硬盘序列号的原代码-.NET教程,C#语言
·取得的IP/用户名等信息-ASP教程,ASP应用
最新文章
·linux操作系统上摄像头的使用小技巧_linux教程
·c#动态生成树型结构的web程序设计_c#应用
·一个联盟该如何来吸引站长加盟_网赚技巧
·童之磊:中文在线的“二次创业”_站长访谈
·实体店铺如何与网店结合提高销售额的经验_站长心得
·20年之后,个人站长发展项目的一些奇思怪想_站长心得
·google adsense的菜鸟常识_google推广
·如何利用google增加你的网站访问量_google推广
·专业的seo,为什么不承诺排名_seo网站优化
·用专业代码给网站增加搜索引擎_站长心得
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭