Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2339354
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2014-11-11 07:32:23

主要涉及转化:

从String转化为document再到xml

从xml转化为document再到String

view plaincopy to clipboardprint?
xml转化为string:

  1. public static String xmlFile2String(String fileName) throws SAXException, IOException, ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException
  2.  
  3.           {
  4.  
  5.                
  6.  
  7.                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  8.  
  9.                   InputSource inputSource = new InputSource(fileName);
  10.  
  11.                   Document document = documentBuilderFactory.newDocumentBuilder().parse(inputSource);
  12.  
  13.                   StringWriter sw = new StringWriter();
  14.  
  15.                   Transformer serializer = TransformerFactory.newInstance().newTransformer();
  16.  
  17.                   serializer.transform(new DOMSource(document), new StreamResult(sw));
  18.  
  19.                   return sw.toString();
  20.  
  21.           }
doc转化为string:

  1. DOMSource domSource = new DOMSource(doc);
  2.  
  3.             StringWriter writer = new StringWriter();
  4.  
  5.             StreamResult result = new StreamResult(writer);
  6.  
  7.             TransformerFactory tf = TransformerFactory.newInstance();
  8.  
  9.             Transformer transformer = tf.newTransformer();
  10.  
  11.             transformer.transform(domSource, result);
  12.  
  13.             System.out.println( writer.toString());
doc转化为xml:

  1. TransformerFactory tFactory = TransformerFactory.newInstance();
  2.  
  3.       Transformer transformer = tFactory.newTransformer();
  4.  
  5.       DOMSource source = new DOMSource(doc);
  6.  
  7.       FileOutputStream outStream = new FileOutputStream( "outFile.xml ");
  8.  
  9.       StreamResult result = new StreamResult(outStream);
  10.  
  11.       transformer.transform(source, result);
doc的初始化:

  1. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  2.  
  3.   DocumentBuilder db = null;
  4.  
  5.   try {
  6.  
  7.   db = dbf.newDocumentBuilder();
  8.  
  9.   } catch (ParserConfigurationException pce) {
  10.  
  11.   System.err.println(pce);
  12.  
  13.   System.exit(1);
  14.  
  15.   }
  16.  
  17.   Document doc = null;
  18.  
  19.   doc = db.newDocument();
用读文件的方式把xml变成string: 

  1. FileInputStream in = new FileInputStream( "DataTest.xml ");
  2.  
  3.       BufferedInputStream bufferin=new BufferedInputStream(in);
  4.  
  5.       byte c[]= new byte[1000];
  6.  
  7.       int n=0;
  8.  
  9.       StringBuffer bs=new StringBuffer();
  10.  
  11.       while ((n=bufferin.read(c))!=-1)
  12.  
  13.       {String temp=new String (c,0,n);//
  14.  
  15.         bs.append(temp);
  16.  
  17.       }
xml转化为string:

  1. public static String xmlFile2String(String fileName) throws SAXException, IOException, ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException

  2.           {

  3.                   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

  4.                   InputSource inputSource = new InputSource(fileName);

  5.                   Document document = documentBuilderFactory.newDocumentBuilder().parse(inputSource);

  6.                   StringWriter sw = new StringWriter();

  7.                   Transformer serializer = TransformerFactory.newInstance().newTransformer();

  8.                   serializer.transform(new DOMSource(document), new StreamResult(sw));

  9.                   return sw.toString();

  10.           }
doc转化为string:

  1. DOMSource domSource = new DOMSource(doc);

  2.             StringWriter writer = new StringWriter();

  3.             StreamResult result = new StreamResult(writer);

  4.             TransformerFactory tf = TransformerFactory.newInstance();

  5.             Transformer transformer = tf.newTransformer();

  6.             transformer.transform(domSource, result);

  7.             System.out.println( writer.toString());
doc转化为xml:

  1. TransformerFactory tFactory = TransformerFactory.newInstance();

  2.       Transformer transformer = tFactory.newTransformer();

  3.       DOMSource source = new DOMSource(doc);

  4.       FileOutputStream outStream = new FileOutputStream( "outFile.xml ");

  5.       StreamResult result = new StreamResult(outStream);

  6.       transformer.transform(source, result);
doc的初始化:

  1. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

  2.   DocumentBuilder db = null;

  3.   try {

  4.   db = dbf.newDocumentBuilder();

  5.   } catch (ParserConfigurationException pce) {

  6.   System.err.println(pce);

  7.   System.exit(1);

  8.   }

  9.   Document doc = null;

  10.   doc = db.newDocument();
用读文件的方式把xml变成string:

  1. FileInputStream in = new FileInputStream( "DataTest.xml ");

  2.       BufferedInputStream bufferin=new BufferedInputStream(in);

  3.       byte c[]= new byte[1000];

  4.       int n=0;

  5.       StringBuffer bs=new StringBuffer();

  6.       while ((n=bufferin.read(c))!=-1)

  7.       {String temp=new String (c,0,n);//

  8.         bs.append(temp);

  9.       }
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


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/luweifeng1983/archive/2009/03/26/4028250.aspx

转自:http://www.cnblogs.com/xianghang123/archive/2010/04/06/1705385.html










阅读(1249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~