XMLBuilder.class 主要是把指定的document.node对象转换成规范的xml字符串。用的是ibm的xml4j解析器.代码如下: package com.ceic.workflow.xml; import Java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import org.w3c.dom.Attr; import org.w3c.dom.document. import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.ibm.xml.parsers.*; /** * Title: 有效XML 字符串生成工具 * Description: 有效XML 字符串生成工具 * Copyright: Copyright (c) 2003 * Company: 国电信息中心 * @author 张治中 * @version 1.0 * 有效XML 字符串生成工具 * 例如: * XmlBuilder build=new XmlBuilder(); * document.nbspdoc=((document.Class.forName("com.ibm.xml. * dom.document.mpl").newInstance()) * .......... * build.printDOMTree(doc); * String xmlString=build.getXmlResult(); * 再把xmlString用XmlOutput类去输出成xml文件. */ public class XmlBuilder { private String lineSeparator="\r"; private String xmlString=""; private int indentLevel=0; protected static String STANDARD_INDENT=" "; private String XmlHeader="<?xml version=\"1.0\" ?>"; private int currentlevel=0; /** * 生成XML字符串. * @param node 要生成字符串的document.其它Node. */ public void printDOMTree(Node node){ printDOMTree(node,indentLevel,false); } /** * 生成XML字符串. * @param node 要生成字符串的document.其它Node. * @param noTop 是否去除头尾,假如为document.象去掉<?xml.../?>头 */ public void printDOMTree(Node node,boolean noTop){ printDOMTree(node,indentLevel,noTop); } /** * 生成XML字符串. * @param node 要生成字符串的document.其它Node. * @param level 节点的深度.(中间变量) * @param noTop 是否去除头尾,假如为document.象去掉<?xml.../?>头 */ private void printDOMTree(Node node,int level,boolean noTop) { int templevel=level; int find=0; short toptype=0; String topvalue=""; int type = node.getNodeType(); switch (type) { // print the document.nbspelement case Node.document.NODE: { find++; if(!noTopfind>1){ xmlString+=XmlHeader+lineSeparator; }else{ toptype=Node.document.NODE; } printDOMTree(((document.node).getdocument.lement(), templevel+1,false); break; } // print element with attributes case Node.ELEMENT_NODE: { find++; if(!noTopfind>1){ currentlevel=templevel; xmlString+=printIndent(templevel); xmlString+=lineSeparator+"<"; xmlString+=node.getNodeName(); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); xmlString+=" " + attr.getNodeName() +"=\"" + attr.getNodevalue() +"\""; } xmlString+=">"+lineSeparator; } else{ toptype=Node.ELEMENT_NODE topvalue="</"+node.getNodeName()+">"+lineSeparator; } NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i),templevel+1,false); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { find++; xmlString+="&"; xmlString+=node.getNodeName(); xmlString+=";"; break; } // print cdata sections case Node.CDATA_SECTION_NODE: { find++; xmlString+="<![CDATA["; xmlString+=node.getNodevalue(); xmlString+="]]>"; break; } // print text case Node.TEXT_NODE: { find++; // String temp=node.getNodevalue(); // if(!temp.equals(" ")&&!temp.equals("\n") // &&!temp.equals("\r")) xmlString+=node.getNodevalue(); break; } // print processing instrUCtion case Node.PROCESSING_INSTRUCTION_NODE: { find++; xmlString+="<?"; xmlString+=node.getNodeName(); String data = node.getNodevalue(); { xmlString+=" "; xmlString+=data; } xmlString+="?>"; break; } } if (type == Node.ELEMENT_NODE) { find++; if(currentlevel!=templevel){ xmlString+=printIndent(templevel); xmlString+=lineSeparator; } xmlString+="</"; xmlString+=node.getNodeName(); xmlString+=">"+lineSeparator; } if(noTop&&toptype==Node.ELEMENT_NODE){ int len=xmlString.length() int tlen=topvalue.length() xmlString=xmlString.substring(0,len-tlen); } } /** * 生成行前的STANDARD_INDENT(一般指空格) * @param num STANDARD_INDENT的个数 * @return String */ private String printIndent(int num){ String temp=""; if(num>0){ for(int i=0;i<num;i++){ temp+=STANDARD_INDENT; } } return temp; } /** * 设定行前的STANDARD_INDENT(一般指空格) * @param indent STANDARD_INDENT的值 */ public void setIndent(String indent){ STANDARD_INDENT=indent; } /** * 获得已经生成的xml字符串.在printDOMTree(Node node)方法后有效 * @return String */ public String getXmlResult(){ return xmlString; } /** * 设定最开始的深度级别(直接影响行前的STANDARD_INDENT(空格)数) * @param level 级别数 */ public void setBeginLevel(int level){ indentLevel=level; } /** * 设定xml文件的xml头 * @param header xml文件xml头。例如:<?xml version=\"1.0\" ?> */ public void setXmlHeader(String header){ XmlHeader=header; } /** * 设定换行符 默认为"\r\n" * @param lineseparator 换行分割符,默认为"\r\n" */ public void setlineSeparator(String lineseparator){ lineSeparator=lineseparator; } } XMLOutput.class 功能是用指定的string或InputStream生成文件(不一定是xml文件)。代码如下: package com.ceic.workflow.xml import org.w3c.dom.*; import java.io.*; import java.util.*; /** * Title: 有效XML 字符串生成xml文件的工具 * Description: 有效XML 字符串生成xml文件的工具 * Copyright: Copyright (c) 2003 * Company: 国电信息中心 * @author 张治中 * @version 1.0 */ public class XmlOutput{ private String objectpath; pri
|