载入 就是指上面的几行xml文件读入生成Document的语句。

解决方案 »

  1.   

    >>读取xml文件后,再输出成new.xml,发现原来在Tab.xml中的注释都没有了!这里可能会产生误解,改一下:读取xml文件后,再输出成new.xml,发现原来Tab.xml中的注释,在new.xml中 都没有了!
      

  2.   

    我这里没有出现楼主描述的情况啊
    不过不知道我用的dom4j是否1.5的测试代码如下://dom4jTest.java
    public class dom4jTest {    public static void main(String[] args) {
            SAXReader xr = new SAXReader();
            //xr.setIgnoreComments(false);
            Document doc = null;
            try {
                doc = xr.read(new File("D:/1.xml"));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            System.out.println(tranToString(doc));
            writeToFile(doc, "GB2312", "D:/2.xml");
        }    public static String tranToString(Document doc) {
            try {
                ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
                XMLWriter writer =
                    new XMLWriter(byteRep, new OutputFormat("", false, "UTF-8"));
                writer.write(doc);
                writer.close();
                return byteRep.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }    public static void writeToFile(
            Document doc,
            String encoding,
            String path) {
            try {
                FileWriter fwriter = new FileWriter(path);
                XMLWriter writer =
                    new XMLWriter(fwriter, new OutputFormat("", false, encoding));
                writer.write(doc);
                writer.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
    }
    //1.xml
    <?xml version="1.0" encoding="GB2312"?><student>
      <name>Tom</name><!-- comment here -->
      <no>001</no>
      <school>wust</school>
    </student>
    //2.xml
    <?xml version="1.0" encoding="GB2312"?>
    <student>
      <name>Tom</name><!-- comment here -->
      <no>001</no>
      <school>wust</school>
    </student>
      

  3.   

    自己解决了:SAXReader xr = new SAXReader("org.apache.xerces.parsers.SAXParser");就行了。