JAVA-DOM4J-XML转化成String字串
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import DefaultAttribute;
import DefaultElement;
Document document = ateDocument();
Element root = document.addElement("response");
Element lst = root.addElement("lst");
Element int1 = lst.addElement("int");
int1.addAttribute("name", "status");
int1.addText(status);
Element int2 = lst.addElement("int");
int2.addAttribute("name", "QTime");
int2.addText(QTime);
Element str1 = root.addElement("str");
str1.addAttribute("name", "core");
str1.addText(coreName);
Element str2 = root.addElement("str");
str2.addAttribute("name", "saved");
str2.addText(solrXmlPath);
// write XML
XMLWriter xmlWriter = null;
StringWriter out = new StringWriter(1024);
OutputFormat format = atePrettyPrint();
format.setEncoding("UTF-8");
try
{
xmlWriter = new XMLWriter(out, format);
xmlWriter.write(document);
ret = String();;
}
catch (UnsupportedEncodingException ex)
{
<("xml encoding error: " + ex);
}
catch (IOException ex)
{
<("xml writing error: " + ex);
}
finally
{
if (xmlWriter != null)
{
try
{
xmlWriter.close();
}
catch (IOException ex)
{
<("error occured when closing XMLWriter: " + ex);
}
}
if (out != null)
{
try
java xml是什么{
{
out.close();
}
catch (IOException ex)
{
<("error occured when closing StringWriter: " + ex); } }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论