Creating XML response in Java

Posted on 2009-12-28
Last Modified: 2013-12-17
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"?>
  <Status code="101" text="Accepted">Acknowledged</Status>


public class TestPost {

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

            String inputString = "";
            String input;
            try {
                  BufferedReader in = new BufferedReader(new FileReader(
                  while ((input = in.readLine()) != null)
                        inputString = inputString + input;

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

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

                  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"));

                  BufferedReader in2 = new BufferedReader(new InputStreamReader(c

                  String inputLine;
                  while ((inputLine = in2.readLine()) != null)

            } catch (Exception e) {




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

        HttpSession session = request.getSession();

        String xml = request.getParameter("xmldoc");
      public void checkIfXMLIsWellFormed(String aXml){
                  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){

            catch(IOException io){

Question by:AjooAli
    LVL 92

    Expert Comment

    whats wrong with out.println(xml); as you have commented out


    Author Comment

    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"?>
      <Status code="101" text="Accepted">Acknowledged</Status>

    Author Comment

    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
    LVL 92

    Accepted Solution

    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

    Author Comment

    Thanks, You suggession did help


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now