Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: saving xml file

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default saving xml file

    hello everyone
    this code should read xml file , edit it and return the new nodes to the same file ...
    It doesn't work ...
    please any help?
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    import java.io.*;
     
     
    public class Register extends HttpServlet{
     
     
     
        static void toXML(Document doc,PrintWriter o)
        {        
            try{
                TransformerFactory xf = TransformerFactory.newInstance();
     
                Transformer xformer = xf.newTransformer();
               // xformer.setOutputProperty(OutputKeys.METHOD, "xml");
                //xformer.setOutputProperty(OutputKeys.INDENT, "yes");
                //xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                //Result out = new StreamResult(new OutputStreamWriter(System.out,"UTF-8"));
     
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(System.out);
                xformer.transform(source, result);             
            }
            catch (TransformerConfigurationException tce) {
                // Error generated by the parser
                System.out.println ("* Transformer Factory error");
                System.out.println("  " + tce.getMessage() );
                // Use the contained exception, if any
                Throwable x = tce;
                if (tce.getException() != null)
                x = tce.getException();
                x.printStackTrace();
            }
            catch (TransformerException te) {
                // Error generated by the parser
                System.out.println ("* Transformation error");
                System.out.println("  " + te.getMessage() );
                // Use the contained exception, if any
                Throwable x = te;
                if (te.getException() != null)
                    x = te.getException();
                x.printStackTrace();
            }
            catch(Exception exception)
            {
                System.out.println(exception);
            }
        }
     
        public void doGet(HttpServletRequest request , HttpServletResponse response)
                throws IOException , ServletException{
     
            PrintWriter out = response.getWriter();
            try{
                out.println("<html><body background=\"images/backG.GIF\" >");
                out.println("<basefont face=\"comic sans ms\" color=\" #7429C7\" />");
     
                 String param_name = request.getParameter("name");       
     
                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = builderFactory.newDocumentBuilder();
                Document document = builder.parse("F:/users.xml");
     
                 Element rootElement = document.getDocumentElement();
                 NodeList users=  rootElement.getElementsByTagName("user");
                 Element User=  (Element) document.getElementsByTagName("Users").item(0);
                 char x = 'n';
                 for(int i=0; i<users.getLength();i++)
                 {
                     Element question_element = (Element) users.item(i);
                      String attribute_name = question_element.getAttribute("name");
     
                      if(param_name.equalsIgnoreCase(attribute_name))
                      {
                          x = 'y';
                          break;
                      }
                 }
                 if(x == 'y')
                     out.println("This Account Is Already Exists .... Try another name please");
                 else
                 {
     
                     Element new_child  = document.createElement("user");
                     User.appendChild(new_child);
                     new_child.setAttribute("name", param_name);            
                     new_child.setAttribute("type", "student");
                     //NodeList mm = User.getElementsByTagName("user");
                     toXML(document, out);
     
                 }
          }        
            catch(Exception e) {
                String errorMessage ="Error making root node: " + e;
                out.println(errorMessage);
                e.printStackTrace();
            }
        }
      public  void doPost(HttpServletRequest request , HttpServletResponse response)throws IOException , ServletException
        {
            doGet(request, response);
        }
     
    }
    thanx anyway


  2. #2
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: saving xml file

    what's the error?

Similar Threads

  1. Replies: 0
    Last Post: April 10th, 2010, 05:52 PM
  2. Help needed regarding saving BLOB in oracle using JSP
    By smsouvik_2008 in forum JDBC & Databases
    Replies: 1
    Last Post: February 26th, 2010, 02:09 AM
  3. Saving an image file (PGM format)
    By Mickey2315 in forum Algorithms & Recursion
    Replies: 3
    Last Post: September 12th, 2009, 01:18 AM
  4. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM