`
海欣_海夜
  • 浏览: 16423 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

XML转换成String

    博客分类:
  • XML
阅读更多
private static String getJdomXmlToString(Document xml){
		String result="";
		Format format =Format.getPrettyFormat();
		format.setEncoding("GB2312");
		XMLOutputter xmloutput =new XMLOutputter(format);
		ByteArrayOutputStream  byteStream =new ByteArrayOutputStream();
		try {
			xmloutput.output(xml, byteStream);
			result=byteStream.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}

//** org.w3c.dom 转化成 String
	private static String getXMLToString(Document xml){
		String result="";
		if(xml!=null){
			StringWriter strWrite =new StringWriter();
			StreamResult strResult=new StreamResult(strWrite);
			TransformerFactory tfac = TransformerFactory.newInstance();
			try {
				Transformer trf=tfac.newTransformer();
				trf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
				trf.setOutputProperty(OutputKeys.INDENT, "yes");
				trf.setOutputProperty(OutputKeys.METHOD, "xml");
				trf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
				trf.transform(new DOMSource(xml.getDocumentElement()), strResult);
				result=strResult.getWriter().toString();
			} catch (Exception e) {				
				e.printStackTrace();
			}
		}
		return result;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics