欢迎光临
我们一直在努力

XML 实用工具类-JSP教程,Java与XML

建站超值云服务器,限时71元/月

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 );

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » XML 实用工具类-JSP教程,Java与XML
分享到: 更多 (0)

相关推荐

  • 暂无文章