Java tutorial
/* * Copyright 2010-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.starflow.engine.xml; import java.io.IOException; import java.io.StringWriter; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; /** * * @author libinsong1204@gmail.com * @version 1.0 */ public class XmlFormat { /** * dom4j?XML'pretty'?.getPrettyStiring(doc,"gb2312"); * * @param xml * ??XML. * @return ??XML. * @see XmlFormat#getPrettyStiring(String, String) */ public static String getPrettyString(String xml) { try { return getPrettyString(xml, "GB2312"); } catch (DocumentException e) { e.printStackTrace(); throw new RuntimeException("Pretty Xml "); } } /** * dom4j?XML'pretty'?. * * @param xml * ??XML. * @param encoding * ??. * @return ??XML.null,???. */ public static String getPrettyString(String xml, String encoding) throws DocumentException { if (xml == null || xml.trim().length() < 1) { return xml; } Document doc = DocumentHelper.parseText(xml); return getPrettyString(doc, encoding); } /** * dom4j'pretty'??dom. * * @param doc * ??dom. * @param encoding * ??. * @return ??XML.null,???. */ public static String getPrettyString(Document doc, String encoding) { StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); if (encoding == null || "".equals(encoding.trim())) { encoding = "GBK"; } format.setEncoding(encoding); XMLWriter xmlwriter = new XMLWriter(writer, format); try { xmlwriter.write(doc); } catch (IOException e) { e.printStackTrace(); } return writer.toString(); } }