package myPackage;
import java.io.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class MyClass {
public static void main(String[] args) {
File f=new File("test.xml");
SAXBuilder saxb=new SAXBuilder();
XMLOutputter op=new XMLOutputter();
try {
Document xmldoc=saxb.build(f);
op.output(xmldoc, System.out);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
==============================================================================
public static String covertNodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(node);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(source, new StreamResult(baos));
return String.valueOf(baos);
}
这样可以了. 不过我把string转换成UTF8后,还是有乱码. 如何处理?
还有个问题,我这样转换,会自动加<?xml version="1.0" encoding="UTF-8"?>这句