Java Dom4j 解决中文乱码问题

	@SuppressWarnings("unchecked")
	@Test
	public void testDocumentById () throws Exception{
		File f = new File("F:/WS_YU/wday/WebContent/FullCanlendar/note.xml");
		SAXReader reader = new SAXReader();
        Document document = reader.read(f);
        
        Element root = document.getRootElement();
        Element e1 = root.elementByID("S2016-02-01");
        System.out.println(e1.attributeValue("title"));
        List<Element> s=root.elements("event");
        for (Element e:s){
        	String id = e.attribute("ID").getValue();
        	String title = e.attribute("title").getValue();
        	System.out.println(id+":"+title);
        }
	}
	
	@Test
	public void testWrite () throws IOException, DocumentException{
		File f = new File("F:/WS_YU/wday/WebContent/FullCanlendar/note.xml");
		
		SAXReader reader = new SAXReader();
        Document document = reader.read(f);
        
        Element root = document.getRootElement();
        
        Element e1 = root.addElement("event");
        e1.addAttribute("ID", "S2016-12-12");
        e1.addAttribute("title", "国庆节");
        
        FileOutputStream fos = new FileOutputStream(f);
        OutputFormat of = OutputFormat.createPrettyPrint();
        of.setEncoding("UTF-8");
        of.setIndent("	");
		XMLWriter writer = new XMLWriter(fos,of);

		writer.write(document);
		writer.flush();
        writer.close();
	}

猜你喜欢

转载自aa80303857.iteye.com/blog/2346480