JAVA操作XML二(转换关系):String --document--xml --docu...

主要涉及转化:

从String转化为document再到xml

从xml转化为document再到String

 

[java] view plain copy
  1. xml转化为string:    
  2.   
  3. public   static   String   xmlFile2String(String   fileName)   throws   SAXException,   IOException,   ParserConfigurationException,   TransformerFactoryConfigurationError,   TransformerException  
  4.   
  5.           {  
  6.   
  7.                
  8.   
  9.                   DocumentBuilderFactory   documentBuilderFactory   =   DocumentBuilderFactory.newInstance();  
  10.   
  11.                   InputSource   inputSource   =   new   InputSource(fileName);  
  12.   
  13.                   Document   document   =   documentBuilderFactory.newDocumentBuilder().parse(inputSource);                
  14.   
  15.                   StringWriter   sw   =   new   StringWriter();  
  16.   
  17.                   Transformer   serializer   =   TransformerFactory.newInstance().newTransformer();  
  18.   
  19.                   serializer.transform(new   DOMSource(document),   new   StreamResult(sw));  
  20.   
  21.                   return   sw.toString();  
  22.   
  23.           }  
  24.   
  25.   doc转化为string:  
  26.   
  27.           DOMSource   domSource   =   new   DOMSource(doc);  
  28.   
  29.             StringWriter   writer   =   new   StringWriter();  
  30.   
  31.             StreamResult   result   =   new   StreamResult(writer);  
  32.   
  33.             TransformerFactory   tf   =   TransformerFactory.newInstance();  
  34.   
  35.             Transformer   transformer   =   tf.newTransformer();  
  36.   
  37.             transformer.transform(domSource,   result);  
  38.   
  39.             System.out.println(   writer.toString());  
  40.   
  41. doc转化为xml:  
  42.   
  43.               TransformerFactory   tFactory   =   TransformerFactory.newInstance();  
  44.   
  45.       Transformer   transformer   =   tFactory.newTransformer();  
  46.   
  47.       DOMSource   source   =   new   DOMSource(doc);  
  48.   
  49.       FileOutputStream   outStream   =   new   FileOutputStream( "outFile.xml ");  
  50.   
  51.       StreamResult   result   =   new   StreamResult(outStream);  
  52.   
  53.       transformer.transform(source,   result);    
  54.   
  55.    
  56.   
  57. doc的初始化:  
  58.   
  59.                   DocumentBuilderFactory   dbf   =   DocumentBuilderFactory.newInstance();    
  60.   
  61.   DocumentBuilder   db   =   null;    
  62.   
  63.   try     {    
  64.   
  65.   db   =   dbf.newDocumentBuilder();    
  66.   
  67.   }   catch   (ParserConfigurationException   pce)   {    
  68.   
  69.   System.err.println(pce);    
  70.   
  71.   System.exit(1);    
  72.   
  73.   }    
  74.   
  75.   Document   doc   =   null;    
  76.   
  77.   doc   =   db.newDocument();    
  78.   
  79.    
  80.   
  81.    
  82.   
  83. 用读文件的方式把xml变成string:  
  84.   
  85.       FileInputStream   in   =   new   FileInputStream( "DataTest.xml ");  
  86.   
  87.       BufferedInputStream   bufferin=new   BufferedInputStream(in);  
  88.   
  89.       byte   c[]=   new   byte[1000];  
  90.   
  91.       int   n=0;  
  92.   
  93.       StringBuffer   bs=new   StringBuffer();  
  94.   
  95.       while   ((n=bufferin.read(c))!=-1)  
  96.   
  97.       {String   temp=new   String   (c,0,n);//                    
  98.   
  99.         bs.append(temp);                      
  100.   
  101.       }  

 

String转成document的请看以下链接

http://www.blogjava.net/hopeshared/archive/2006/07/06/56919.aspx

有关经XSL文件转换为XML的请看:

http://blog.csdn.net/luweifeng1983/archive/2009/03/25/4022431.aspx

转载于:https://my.oschina.net/zhuzihasablog/blog/149483

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值