服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

学习java&xml心得(2)


之前内容请参看:好东西大家分享:java&xml的学习心得(1)
   xmltooltest.class实现了对xml文档的操作。包括建立一个新xml document.档结构及内容或从指定xml文件或的document.构和内容,output到指定的xml文件(或更新xml文件),获取和设定指定节点名的指定属性(或属性列表),在指定节点添加子节点、删除子节点、编辑子节点(先删除在添加,待完善),支持在文档中定位(下标定位,和查询定位,如:定位到test节点的 name attribute的value="张治中"的节点,有简单的复合查询定位,暂时是全and的关系. :) ),定位后可以和全面的操作联合使用(有点数据库的影子,和自己想做的应用有关)。后来又根据它做了一个数据库和xml对导的例子。代码如下:

package com.ceic.workflow.xml.tool;
import com.ceic.workflow.xml.*;
import org.w3c.dom.*;
/**
* title: xml处理工具的 运算和实现部分的接口
* description: xml处理工具的 运算和实现部分的接口
* copyright: copyright (c) 2003
* company: 国电信息中心
* @author 张治中
* @version 1.0
* xml处理工具的 运算和实现部分 的接口
* xmltool的实现子类,其中的xml解析器从xmlparserfactory中获得.
* 没有说明的方法和参数参见xmltool接口.
*/
public class xmltooltest implements xmltool
{
private xmlparser parser;
private string parsertype=
xmlparserfactory.getdefaulttype();
private string parserclassname="";
private int index=0;
private string markname="";
private document.nbspdoc;
private int defaultbegin=0;
private string encoding="utf8";
private string filepath="";
private boolean enablemakeup=false;
private string header=" ";
public xmltooltest()
{

}
public object build(string path, boolean vali)
{
if(path!=null&&path.length() >0){
parser=xmlparserfactory.getxmlparser
(parsertype,parserclassname);
if(parser!=null){
doc=(document.parser.parse(path,vali);
filepath=path;
return doc;
}
}
return null;
}
public object build(string xmlstring)
{
if(xmlstring!=null&&xmlstring.length() >0){
parser=xmlparserfactory.getxmlparser
(parsertype,parserclassname);
if(parser!=null){
doc=(document.parser.parsestring(xmlstring);
filepath="";
return doc;
}
}
return null;
}
public void setdocument.ource(document.nbspdocs){
doc=docs;
}

public void setparser(string parsername, string classname)
{
if(parsername!=null&&parsername.length() >0){
parsertype=parsername;
parserclassname=classname;
}
}

public void setproperty(string nodename,string
propertyname,string value,boolean setall){
try{
nodelist list=doc.getelementsbytagname(nodename);
if(list.getlength() >0&&value!=null&&value.length()>0)
{
if(setall){
for(int i=0;i

if(propertyname!=null&&propertyname.length()>0)
{

((element)list.item(i)).setattribute
(propertyname,value);
}
else{
list.item(i).setnodevalue(value);
}
}

}else{

if(propertyname!=null&&propertyname.length()>0){
if(nodename.equals(markname)){
((element)list.item(index)).setattribute
(propertyname,value);
}
else{
((element)list.item(defaultbegin)).
setattribute(propertyname,value);
}
}else{

if(nodename.equals(markname)){
list.item(index).setnodevalue(value);
}
else{
list.item(defaultbegin).setnodevalue(value);
}

}
}
}
}catch(exception ee){
system.out.println("设定"+nodename+"节点的"+
propertyname+"属性出错");
}
}
public string getproperty(string nodename,
string propertyname)
{
try{
nodelist list=doc.getelementsbytagname(nodename);
if(list.getlength() >0){
if(propertyname!=null&&propertyname.length() >0){
if(nodename.equals(markname)){
return ((element)list.item(index)).getattribute
(propertyname);
}else{
return ((element)list.item(defaultbegin)).
getattribute(propertyname);
}
}else{
if(nodename.equals(markname)){
return ((element)list.item(index)).getnodevalue()
}else{
return ((element)list.item(defaultbegin)).
getnodevalue()
}
}
}
return null;
}catch(exception ee){
system.out.println("getproperty("+nodename+","+
propertyname+");出错"+ee.getmessage());
return null;
}
}
public string[] getpropertys(string nodename,
string propertyname)
{
try{
string[] temp;
nodelist list=doc.getelementsbytagname(nodename);
if(list.getlength() >0){
if(propertyname!=null&&propertyname.length() >0){
temp=new string[list.getlength()];
for(int i=0;i
temp[i]=((element)list.item(i)).
getattribute(propertyname);
}
return temp;
}else{
temp=new string[list.getlength()];
for(int i=0;i
temp[i]=((element)list.item(i)).getnodevalue()
}
return temp;
}
}
return null;
}catch(exception ee){
system.out.println("getpropertys("+nodename+","+
propertyname+");出错");
return null;
}
}
public string getparent(string returnattr){
string tempreturn="";
string tempnode=markname
int tempindex=index
nodelist list=doc.getelementsbytagname(markname);
node node=list.item(index);
if(returnattr!=null&&returnattr.length() >0){
tempreturn=((element)node.getparentnode()).
getattribute(returnattr);
}else{
tempreturn=node.getparentnode().getnodevalue()
}
return tempreturn;
}


public void setformat(string xmltoolname,
string classname){}

public void addnode(string parent, node nodename,
boolean addall)
{
try{
nodelist list=doc.getelementsbytagname(parent);

if(list.getlength() >0){
if(addall){
for(int i=0;i
int type=nodename.getnodetype()
switch(type){
case node.attribute_node: {
((element)list.item(i)).setattribute(((attr)
nodename).getname() ,((attr)nodename).getvalue());
}
default:{
list.item(i).appendchild(nodename);
}
}
}
}else{
if(parent.equals(markname)){
int type=nodename.getnodetype()
switch(type){
case node.attribute_node: {
((element)list.item(index)).setattribute
(((attr)nodename).getname() ,((attr)nodename).getvalue());
}
default:{
list.item(index).appendchild(nodename);
}
}
}else{

int type=nodename.getnodetype()
switch(type){
case node.attribute_node: {
((element)list.item(defaultbegin)).
setattribute(((attr)nodename).getname() ,
((attr)nodename).getvalue());
}
default:{
list.item(defaultbegin).appendchild(nodename);
}
}

}
}

}
}catch(exception e){
system.out.println("增加节点出错");
}
}
public void delnode(string parent,string nodename,
boolean delall)
{
try{
nodelist list=doc.getelementsbytagname(parent);
if(list.getlength() >0){
if(delall){
for(int i=0;i
try{
((element)list.item(i)).removeattribute(nodename);
}catch(exception ee){}

nodelist tlist=list.item(i).getchildnodes()
if(tlist.getlength() >0){
for(int k=0;k
if(nodename.equals(tlist.item(k).getnodename())){
short type=tlist.item(k).getnodetype()
if(type==node.attribute_node){
((element)list.item(i)).removeattribute
(nodename);
}
else list.item(i).removechild(tlist.item(k));
break;
}
}
}

}
}else{
if(parent.equals(markname)){
try{
((element)list.item(index)).removeattribute(nodename);
}catch(exception ee){}

nodelist tlist=list.item(index).getchildnodes()
if(tlist.getlength() >0){
for(int k=0;k
if(nodename.equals(tlist.item(k).
getnodename())){
short type=tlist.item(index).getnodetype()
if(type==node.attribute_node){
((element)list.item(index)).
removeattribute(nodename);
}
else list.item(index).removechild
(tlist.item(k));
break;
}
}
}
}else{
try{
((element)list.item(defaultbegin)).removeattribute
(nodename);
}catch(exception ee){}

nodelist tlist=list.item(defaultbegin).
getchildnodes()
if(tlist.getlength() >0){
for(int k=0;k
if(nodename.equals(tlist.item(k).
getnodename())){
short type=tlist.item(index).
getnodetype()
if(type==node.attribute_node){
((element)list.item(defaultbegin)).
removeattribute(nodename);
}
else list.item(defaultbegin).
removechild(tlist.item(k));
break;
}
}
}

}
}
}
}catch(exception e){
system.out.println("删除节点出错");
}
}
public void editnode(string parent,string nodeold,
node nodenew,boolean editall)
{
try{
nodelist list=doc.getelementsbytagname(parent);

if(list.getlength() >0){
if(editall){
for(int i=0;i
nodelist tlist=list.item(i).getchildnodes()
if(tlist.getlength() >0){
for(int k=0;k
if(nodeold.equals(tlist.item(k).getnodename())){
list.item(i).replacechild(tlist.item(k),nodenew);
break;
}
}
}
}
}else{
if(parent.equals(markname)){
nodelist tlist=list.item(index).getchildnodes()
if(tlist.getlength() >0){
for(int k=0;k
if(nodeold.equals(tlist.item(k).getnodename())){
list.item(index).replacechild(tlist.item(k),
nodenew);
break;
}
}
}

}else{


nodelist tlist=list.item(defaultbegin).
getchildnodes()
if(tlist.getlength() >0){
for(int k=0;k
if(nodeold.equals(tlist.item(k).getnodename())){
list.item(defaultbegin).replacechild
(tlist.item(k),nodenew);
break;
}
}
}


}
}
}
}catch(exception e){
system.out.println("替换节点出错");
}
}
public boolean setmark(int index)
{
nodelist list=doc.getelementsbytagname(markname);
if(list.getlength() >0&&index<=list.getlength()-
1&&index>=0 ) {
index=index;
return true;
}
return false;
}


public boolean setmark(string name,string value)
{
if(value!=null&&value.length() >0){
try{
int i=0;
nodelist list=doc.getelementsbytagname(markname);
boolean attrs=false;
boolean find=false;
if(name!=null&&name.length() >0) attrs=true;
if(list.getlength() >0){
for(i=0;i
if(attrs){
if(value.equals(((element)list.item(i)).
getattribute(name))){
find=true;
break;
}
}
else{
if(value.equals(list.item(i).getnodevalue())){
find=true;
break;
}
}
}
if(find){
index=i;
return true;
}else{
return false;
}
}
}catch(exception e){
system.out.println("setmark(string name,
string value)无效");
return false;
}
}
return false;
}
public boolean setmarkadv(string[] name,string[] value,
string operation){
int tempindex=index;
try{
if(name!=null&&name.length >0&&value!=null&&value.
length >0&&name.length ==value.length){
if(operation==null||operation.length() ==0)
operation ="and";
if(!operation.equals("and")&&!operation.equals("or"))
operation="and";
if(operation.equals("and")){
boolean find=true;
int[] results=new int[name.length];
for(int i=0;i
if(setmark(name[i],value[i])){
results[i]=index
if(i>0){
if(results[i]!=results[i-1]){
find=false;
break;
}
}
}else{
find=false;
break;
}
}
if(find){
return true;
}else{
index=tempindex;
return false;
}
}else{
boolean thefind=false;
for(int j=0;j
if(setmark(name[j],value[j])){
thefind=true;
break;
}
}
if(thefind){
return true;
}else{
return false;
}
}
}

return false;
}catch(exception ex){
system.out.println("setmarkadv(string[] name,
string[] value,string operation)出错:"+ex.getmessage());
index=tempindex;
return false;
}
}
public void setmarksign(string nodename)
{
markname=nodename;
}
public document.nbspcreatedocument.){
try{
document.nbspnode=((document.class.forname
("com.ibm.xml.dom.document.mpl").newinstance())
return node;

}catch(exception ex){
system.out.print("找不到com.ibm.xml.dom.document.mpl");
return null;
}
}
public element createelement(string name){
return doc.createelement(name);
}
public comment createcomment(string name){
return doc.createcomment(name);
}
public text createtext(string name){
return doc.createtextnode(name);
}
public attr createattr(string name){
return doc.createattribute(name)
}
public void output(string path){
output(path,encoding,doc,false);
}
public void output(){
output(filepath,encoding,doc,false);
}
public void setencoding(string encod){
encoding=encod;
}
public void isenablemakeup(){
enablemakeup=true;
}
public void isnotenablemakeup(){
enablemakeup=false;
}
public void output(string path,node docu){
output(path,encoding,docu,false);
}
public void output(string path,node docu,boolean notop){
output(path,encoding,docu,notop);
}
public void output(string path,string nodename,
boolean notop){
nodelist list=doc.getelementsbytagname(nodename);
node tempnode;
if(list.getlength() >0){
if(nodename.equals(markname)){
tempnode=list.item(getindex());
}else{
tempnode=list.item(defaultbegin);
}
output(path,encoding,tempnode,notop);
}
}
public void output(string path,string nodename,
int indexs,boolean notop){
nodelist list=doc.getelementsbytagname(nodename);
node tempnode;
if(list.getlength() >0){
if(indexs<=list.getlength() -1){
tempnode=list.item(indexs);
}
else{
if(nodename.equals(markname)){
tempnode=list.item(getindex());
}else{
tempnode=list.item(defaultbegin);
}
}
output(path,encoding,tempnode,notop);
}
}
private void output(string path,string encod,
node docu,boolean notop){
try{
if(path!=null&&path.length()>0){
filepath=path;
}
if(encod!=null&&encod.length() >0){
encoding =encod;
}
xmlbuilder builder=new xmlbuilder();
if(!enablemakeup){
builder.setindent("");
builder.setlineseparator("");
}
builder.setxmlheader(header);
builder.printdomtree(docu,notop);

string result=builder.getxmlresult()

xmloutput out=new xmloutput(filepath);
out.setencoding(encoding);
out.output(result);
}catch(exception ee){
system.out.println(ee.getmessage());
system.out.println("写入xml文件:"+filepath+" 出错");
}
}
public int getindex(){
return index;
}
public string getcurrentnodename(){
return markname
}
public document.nbspgetdocument.ource(){
return doc
}
public void setheader(string header){
if(header!=null&&header.length() >0){
header=header;
}
}
}
   xmltoolfactory.class 代码如下:

package com.ceic.workflow.xml;

import com.ceic.workflow.xml.tool.xmltooltest;
import java.util.*;
/**
* xmltool的工厂类。为其它类提供xml处理工具的 运算和实现部分的类。
*/
public class xmltoolfactory
{
private static hashtable table;
private static string defaulttype;
private static string ower;
/**
* @roseuid 3ecc2fc101bf
*/
private xmltoolfactory()
{

}
/**
* 获得一个常用xmltool.
* @return com.ceic.workflow.xml.xmltool
* @roseuid 3ecc2243019b
*/
public static xmltool getxmltool()
{
try{
return ((xmltool)class.forname(table.get(ower).
tostring()).newinstance());
}catch(exception ee){
system.out.println("获得常用xmltool出错");
return null;
}
}
/**
* 获得一个xmltool.通过参数获得xmltool的一个实现子类.
* @param type - xmltool已实现子类在xmltoolfactory中的type名
* @param classname -
* xmltool实现子类的class名(包含package名),
* 如com.ceic.workflow.xml.tool.xmltooltest
* .默认为null或空字符串。
* @return com.ceic.workflow.xml.xmltool
* @roseuid 3ecc2243019b
*/
public static xmltool getxmltool(string type,
string classname)
{
try{
if(type==null||type.length() <=0){
type=defaulttype;
}
if(table.containskey(type)){
return ((xmltool)class.forname(table.get(type).
tostring()).newinstance());
}
if(classname!=null&&classname.length() >0){
try{
xmltool temp=((xmltool)class.forname(classname).
newinstance());
if(type!=null&&type.length() >0){
table.put(type,classname);
}
return temp;
}catch(exception ee){
system.out.println(ee.getmessage() );
system.out.println("指定的xmltool不存在");
return null;
}
}
return null;
}catch(exception e){
system.out.println(e.getmessage() );
system.out.println("指定的xmltool不存在");
return null;
}
}
public static string getdefaulttype(){
return defaulttype;
}

static
{
table=new hashtable();
table.put("test","com.ceic.workflow.xml.tool.
xmltooltest");
table.put("show","com.ceic.workflow.xml.tool.
xmltoolshow");
defaulttype="test";
ower="show";
}
}

扫描关注微信公众号