public
Void
CreateXML (String filename) {
Document doc = org.dom4j.documenthelper.createdocument ();
Element root = doc.addelement ("book");
Root.addattribute ("Name", "My Book");
ELEMENT CHILDTMP;
ChildTMP = root.addelement ("price");
ChildTmp.Settext ("21.22");
ELEMENT WRITER = root.addelement ("author");
Writer.Settext ("Li Si");
Writer.Addattribute ("ID", "001");
Try {
Org.dom4j.io.xmlwriter xmlwriter = new org.dom4j.io.xmlwriter
New FileWriter (FileName);
XMLWRITER.WRITE (DOC);
XMLWriter.Close ();
}
Catch (Exception E) {
System.out.println (e);
}
} The output used in the above code is the output of the FileWriter object. This is why the subclass inherited by the Writer class inherited from the Writer class did not provide the encoding format processing, so the DOM4J will not process the correct format of the output file. At this time, the files saved at this time are saved with the default encoding of the system. The default code of Java under the Chinese version of WINDOW is GBK, that is, although we identified the XML to be saved as UTF-8 format but actually The file is saved in GBK format, so this is why we can use GBK, GB2312 encoding to generate an XML file that can be parsed correctly, and the file generated in UTF-8 format cannot be parsed by the XML parser. Ok, now we have found the reason, let's find a solution. First let's see how DOM4J implements encoding processing);
Public XMLWriter (OutputStream Out) throws unsupportedEncodingexception {
//System.out.println ("in outputstream ");
this.format = default_format;
This.writer = CREATEWRITEWRITER (OUT, FORMAT.GETENCODING ());
THIS.AUTOFLUSH = TRUE;
NamespaceStack.push (namespace.no_namespace);
}
Public XMLWriter (OutputStream out, OutputFormat Format) throws unsupportedEncodingexception {
//System.out.println ("in outputstream, outputformat ");
this.format = format;
This.writer = CREATEWRITEWRITER (OUT, FORMAT.GETENCODING ());
THIS.AUTOFLUSH = TRUE;
NamespaceStack.push (namespace.no_namespace);
}
/ **
* Get An OutputStreamWriter, Use preferred encoding.
* /
Protected Writer CreateWriter (String Encoding) throws unsupportedEncodingexception {return new bufferedwriter
New OutputStreamWriter (Outstream, Encoding)
);
}
From the above code. We can see that the DOM4J has no complicated processing on the encoding, complete through the functionality of Java itself. So we should not directly assign a Writer object directly when building XMLWRITER using DOM4J, should be built directly to it directly, but should be built through an OutputStream sub-objects. That is to say in our code, you should not use the FileWriter object to build an XML document, and you should use the FileoutPutStream object to build, so you can modify the code: public void createXML (String filename) {
Document doc = org.dom4j.documenthelper.createdocument ();
Element root = doc.addelement ("book");
Root.addattribute ("Name", "My Book");
ELEMENT CHILDTMP;
ChildTMP = root.addelement ("price");
ChildTmp.Settext ("21.22");
ELEMENT WRITER = root.addelement ("author");
Writer.Settext ("Li Si");
Writer.Addattribute ("ID", "001");
Try {// Note the modification here
Org.dom4j.io.xmlwriter xmlwriter = new org.dom4j.io.xmlwriter
New FileOutputStream (filename)
XMLWRITER.WRITE (DOC);
XMLWriter.Close ();
}
Catch (Exception E) {
System.out.println (e);
}
} The problem of issues to this DOM4J is a paragraph, and I hope to use other friends for this article.