Servlet修改XML

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class WriteXml extends HttpServlet {
        
         protected void processRequest(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
                response.setContentType( "text/html;charset=UTF-8");
                request.setCharacterEncoding( "utf-8");
                PrintWriter out = response.getWriter();
                 try {

                        String LayerInfo = request.getParameter( "LayerInfo");
                        ServletContext servletC = getServletContext();
                        String FilePath = servletC.getRealPath( "/SysSet.xml");
                        out.println(LayerInfo);
                     // xmlUpdateDemo(FilePath,"Message","m","dddddddddd",out);
                }
                 finally {
                        out.close();
                }
        }    


public boolean doc2XmlFile(Document document,String filename)
        {
                 boolean flag = true;
                 try
                {
                         TransformerFactory tFactory = TransformerFactory.newInstance();
                         Transformer transformer = tFactory.newTransformer();
                         DOMSource source = new DOMSource(document);
                         StreamResult result = new StreamResult( new File(filename));
                         transformer.transform(source, result);
                 }
                 catch(Exception ex)
                 {
                         flag = false;
                         ex.printStackTrace();
                 }
                 return flag;
        }


public void xmlUpdateDemo(String FileName,String NodeName,String ChildName,String ChildText,PrintWriter out)
        {
                 try
                {
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder builder=factory.newDocumentBuilder();
                        Document document = builder.parse( new File(FileName));
                        Node root=document.getDocumentElement();
                        NodeList ftpnodes = root.getChildNodes();
                         if(root.hasChildNodes())
                        {
                                 for ( int i=0;i<ftpnodes.getLength();i++)
                                    {    
                                                 if(ftpnodes.item(i).hasChildNodes() && ftpnodes.item(i).getNodeName().equals(NodeName))
                                                {
                                                     //    out.println(ftpnodes.item(i).getChildNodes().getLength());
                                                     NodeList ftplist = ftpnodes.item(i).getChildNodes();
                                                         for( int j = 0; j < ftplist.getLength(); j++)
                                                        {
                                                             // out.println(ftplist.item(j).getNodeName());
                                                                 if(ftplist.item(j).getNodeName().equals(ChildName))
                                                                {
                                                                        ftplist.item(j).getFirstChild().setNodeValue(ChildText);
                                                                }
                                                        }
                                                }
                                    }
                        }
                        doc2XmlFile(document,FileName);
                }
                 catch(org.xml.sax.SAXException s)
                {

                }
                 catch(ParserConfigurationException p)
                {

                }
                 catch(IOException e)
                {

                }
        }

你可能感兴趣的:(xml,servlet,职场,休闲)