XML : xml string to xml document in java

I have a java web service which send data as a string xml as below,

  @WebMethod(operationName = "getAngels")      public String getAngels(@WebParam(name = "id") String id, @WebParam(name = "type") String type) {          try {              String xml = "<?xml version='1.0'?><Services>";              Connection conn = broomley.getConnection();              if (id == null) {                  id = "";              }              if (type == null) {                  type = "";              }                String query = "";              if ("".equals(id) && !"".equals(type)) {                  query = "SELECT a.Angelid , a.name , a.email , s.availability , s.charges , s.description , s.images , s.location , s.type FROM app.ANGELS AS a , app.SERVICES AS s WHERE a.Angelid = s.Angelid and s.type LIKE '%" + type + "%'";              } else if (!"".equals(id) && "".equals(type)) {                  query = "SELECT a.Angelid , a.name , a.email , s.availability , s.charges , s.description , s.images , s.location , s.type FROM app.ANGELS AS a , app.SERVICES AS s JOIN app.ANGELS ON app.ANGELS.Angelid = s.Angelid WHERE s.Angelid = " + id + " and a.Angelid = " + id + "";              } else if ("".equals(id) && "".equals(type)) {                  query = "SELECT a.Angelid , a.name , a.email , s.availability , s.charges , s.description , s.images , s.location , s.type FROM app.ANGELS AS a , app.SERVICES AS s JOIN app.ANGELS ON app.ANGELS.Angelid = s.Angelid WHERE s.Angelid = a.Angelid ";              }                PreparedStatement statement = conn.prepareStatement(query);                ResultSet rs = statement.executeQuery();                while (rs.next()) {                  xml += "<Angel id='" + rs.getString("Angelid") + "'type ='" + rs.getString("type") + "'>";                    xml += "<name>" + rs.getString("name") + "</name>";                  xml += "<email>" + rs.getString("email") + "</email>";                  xml += "<service>";                  xml += "<location>" + rs.getString("location") + "</location>";                  xml += "<availability>" + rs.getString("availability") + "</availability>";                  xml += "<description>" + rs.getString("description") + "</description>";                  xml += "<charges>" + rs.getString("charges") + "</charges>";                  xml += "<images>" + rs.getString("images") + "</images>";                  xml += "</service>";                    xml += "</Angel>";              }              xml += "</Services>";                return xml;          } catch (Exception ex) {              System.out.println(ex.getMessage());              return ex.getMessage();          }    

In the cliend side service I get the string, the coding is as below,

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)              throws ServletException, IOException {          response.setContentType("text/html;charset=UTF-8");          try (PrintWriter out = response.getWriter()) {              String id = request.getParameter("id");              String type = request.getParameter("type");              String reslt = getAngels(id,type);              out.println(reslt);          }      }        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">      /**       * Handles the HTTP <code>GET</code> method.       *       * @param request servlet request       * @param response servlet response       * @throws ServletException if a servlet-specific error occurs       * @throws IOException if an I/O error occurs       */      @Override      protected void doGet(HttpServletRequest request, HttpServletResponse response)              throws ServletException, IOException {          processRequest(request, response);      }        /**       * Handles the HTTP <code>POST</code> method.       *       * @param request servlet request       * @param response servlet response       * @throws ServletException if a servlet-specific error occurs       * @throws IOException if an I/O error occurs       */      @Override      protected void doPost(HttpServletRequest request, HttpServletResponse response)              throws ServletException, IOException {          processRequest(request, response);      }        /**       * Returns a short description of the servlet.       *       * @return a String containing servlet description       */      @Override      public String getServletInfo() {          return "Short description";      }// </editor-fold>        private String getAngels(java.lang.String id, java.lang.String type) {          // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.          // If the calling of port operations may lead to race condition some synchronization is required.          brley.BroomleyWebService port = service.getBroomleyWebServicePort();              return port.getAngels(id, type);    }    

I get the out put like below as a string,

Shamila Sallaysallay@gmail.comBroomleyMonday100dogWalker3.jpgShamila Sallaysallay@gmail.comBroomleyTuesdayTake care of granny as my own granny200grannySitter.jpgShamila Sallaysallay@gmail.comBroomley100babySitter15.jpgShalisha Sallayshali@gmail.comBroomleynull50babySitter13.jpgMayur Vindivindi@gmail.comBroomleyTuesdaynull140grannySitter3.jpgColumbs Derbyderby@gmail.comBroomleyMondayTake care of the cat as a child.50catsitter5.jpg

I want to show it like a well formatted xml like below,

  <?xml version='1.0'?><Services>      <Angel id='1'type ='dogwalker'>          <name>Shamila Sallay</name>          <email>sallay@gmail.com</email>          <service>              <location>Broomley</location>              <availability>Monday</availability>              <description></description>              <charges>100</charges>              <images>dogWalker3.jpg</images>          </service>      </Angel>      <Angel id='1'type ='grannysitter'>          <name>Shamila Sallay</name>          <email>sallay@gmail.com</email>          <service>              <location>Broomley</location>              <availability>Tuesday</availability>              <description>Take care of granny as my own granny</description>              <charges>200</charges>              <images>grannySitter.jpg</images>          </service>      </Angel>      <Angel id='1'type ='babysitter'>          <name>Shamila Sallay</name>          <email>sallay@gmail.com</email>          <service>              <location>Broomley</location>              <availability></availability>              <description></description>              <charges>100</charges>              <images>babySitter15.jpg</images>          </service>      </Angel>      <Angel id='2'type ='babysitter'>          <name>Shalisha Sallay</name>          <email>shali@gmail.com</email>          <service>              <location>Broomley</location>              <availability></availability>              <description>null</description>              <charges>50</charges>              <images>babySitter13.jpg</images>          </service>      </Angel>      <Angel id='3'type ='grannysitter'>          <name>Mayur Vindi</name>          <email>vindi@gmail.com</email>          <service>              <location>Broomley</location>              <availability>Tuesday</availability>              <description>null</description>              <charges>140</charges>              <images>grannySitter3.jpg</images>          </service>      </Angel>      <Angel id='4'type ='catsitter'>          <name>Columbs Derby</name>          <email>derby@gmail.com</email>          <service>              <location>Broomley</location>              <availability>Monday</availability>              <description>Take care of the cat as a child.</description>              <charges>50</charges>              <images>catsitter5.jpg</images>          </service>      </Angel>  </Services>    

I have done this in C# and php successfully, but I don't know how to do it in java. Please help.

No comments:

Post a Comment