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;
}
XML转换成String
最新推荐文章于 2022-09-14 11:21:56 发布