服务热线:13616026886

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

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

java 与 xml 结合使用的心得体会(三)


  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(!notop||find>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(!notop||find>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

扫描关注微信公众号