Creating XML response in Java

Hi,
I've writen this code which sends XML from one server to another. I need to write code which will generate XML repsonse and sent is back to client.... Below is is my client and Server code...
The line in my server code where it says //Generate the XML response back. will send response back to clint after building the XML
XML Response to be build:
<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
  <Status code="101" text="Accepted">Acknowledged</Status>
</response>

Code:
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class TestPost {

      /**
       * @param args
       */
      @SuppressWarnings("deprecation")
      public static void main(String[] args) {
            // TODO Auto-generated method stub

            String inputString = "";
            String input;
            try {
                  BufferedReader in = new BufferedReader(new FileReader(
                              "c:\\Java_J2ee\\MyXMLFile.xml"));
                  while ((input = in.readLine()) != null)
                        inputString = inputString + input;

                  System.out.println("Input String is " + inputString);
                  in.close();

                  URL ASNServerURL = new URL("http://localhost:8080/ABCMockServer");
                  HttpURLConnection c = (HttpURLConnection) (ASNServerURL
                              .openConnection());
                  c.setDoOutput(true);

                  PrintWriter out = new PrintWriter(c.getOutputStream());
                  // Here's whether the parameter is set.
                  // out.println("xmldoc=" + URLEncoder.encode(inputString));
                  out.println("xmldoc=" + URLEncoder.encode(inputString, "UTF-8"));
                  out.close();

                  BufferedReader in2 = new BufferedReader(new InputStreamReader(c
                              .getInputStream()));

                  String inputLine;
                  while ((inputLine = in2.readLine()) != null)
                        System.out.println(inputLine);
                  in2.close();

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

}
 Server CODE
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      
            System.out.println("Inside ASNMockServer");
            response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();

        String xml = request.getParameter("xmldoc");
        checkIfXMLIsWellFormed(xml);
        //out.println(xml);
      
      }
      
      
      public void checkIfXMLIsWellFormed(String aXml){
            
            try{
                  
                  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                  InputSource source = new InputSource( new StringReader(aXml));
                  Document document = factory.newDocumentBuilder().parse(source);
                  
                  System.out.println("XML Is well-formed");
                  
                  //Generate the XML response back
                  
                  
            } catch(ParserConfigurationException e){
                  
                        System.out.println( aXml + "Is not well-formed");
                  
            }
            catch(SAXException se){
                  
                        System.out.println(se.getMessage());
                    

            }
            catch(IOException io){
                  
                  System.out.println(io.getMessage());
            }
            
      }



AjooAliAsked:
Who is Participating?
 
objectsCommented:
I see what you mean. What you would do is create a new DOM Document and add the required nodes. Then use the following to write the xml to the response

http://helpdesk.objects.com.au/java/how-to-save-a-xml-dom-document-to-a-file
0
 
objectsCommented:
whats wrong with out.println(xml); as you have commented out

0
 
AjooAliAuthor Commented:
Thanks,
Nothing wrong with it.. What I'm trying to do is to create a new XML response and send it back to Client.

XML Response:
<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
  <Status code="101" text="Accepted">Acknowledged</Status>
</response>
 
0
 
AjooAliAuthor Commented:
Thanks< i know what You mean,  I can set the XML as a string in the out.println(xml);  I think that should help

Let me try
0
 
AjooAliAuthor Commented:
Thanks, You suggession did help

Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.