DOM 写出XML

String servletPath = request.getSession().getServletContext().getRealPath("");
			System.out.println(servletPath);
			File f=new File(servletPath+"\\data.xml"); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
	        DocumentBuilder builder = factory.newDocumentBuilder();
	        
	        Document document = builder.parse(f);
	        Element element = document.getDocumentElement();
	        NodeList bookNodes = element.getElementsByTagName("dials");  
	        bookNodes.item(0).getChildNodes().item(0).getAttributes().getNamedItem("value").setNodeValue("10");
	       
	        //写出XML
	        TransformerFactory tf = TransformerFactory.newInstance();
	        Transformer transformer = tf.newTransformer();//此实例可以用于处理来自不同源的 XML,并将转换输出写入各种接收器。
	        DOMSource source = new DOMSource(document);//创建带有 DOM 节点的新输入源
	        transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");//设置转换中实际的输出属性
		    transformer.setOutputProperty(OutputKeys.INDENT, "yes");//设置转换中实际的输出属性
		    
		    PrintWriter pw = new PrintWriter(new FileOutputStream("src/testDOM.xml"));
//	        StreamResult result = new StreamResult(pw);//从字节流构造 StreamResult 充当转换结果的持有者,可以为 XML、纯文本、HTML 或某些其他格式的标记
	        StreamResult result = new StreamResult(response.getOutputStream());
	        transformer.transform(source, result);//将 XML Source 转换为 Result
 

你可能感兴趣的:(html,xml,F#)