dom4j生成xml文件

Document document = DocumentHelper.createDocument();
		
		document.addDocType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd");
		
		Element plistElement = document.addElement("plist");
		
		plistElement.addAttribute("version", "1.0");
		
		Element dictElement = plistElement.addElement("dict");
		
		Element keyElement = dictElement.addElement("key");
		
		keyElement.setText("PayloadContent");

File file = new File(ServletActionContext.getServletContext().getRealPath(plistFileString));
		
			Writer wr = new OutputStreamWriter(new java.io.FileOutputStream(file),"UTF-8");  
			OutputFormat format = OutputFormat.createPrettyPrint();    
			format.setEncoding("UTF-8");
			XMLWriter xml = new XMLWriter(wr,format);
			xml.write(document);
			xml.close();
			wr.close();

 

生成后的xml文件格式:





	PayloadContent

 

遇到的问题:

xml文件必须采用OutputStreamWriter 方式输出,如果采用FileWrite 输出,文件内容是utf-8,但是文件格式是ANSI,访问时就会出现乱码情况。

你可能感兴趣的:(javaee)