import java.io.*;
import java.io.Reader;
import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.net.*;
import org.xml.sax.*;

public class HelloWorldExample extends HttpServlet{

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
 URL url=new URL("http://cs-server.usc.edu:10724/my1.php");
 URLConnection urlConnection=url.openConnection();
 BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String xmldata;
        while ((xmldata = in.readLine()) != null) 
            {
            //out.println(xmldata);
         }
         //in.close();
    try{        
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is=new InputSource();
is.setCharacterStream(new StringReader(xmldata));
Document document = builder.parse(is);
out.println("HELLLLLLLLLLLLLO");

Element root=document.getDocumentElement();
root.normalize();
String jsondata="";
root.getElementsByTagName("results");
jsondata+=jsondata+"{\"results\":{\"result\":[{";
NodeList results=root.getElementsByTagName("result");
    for(int i=0;i<results.getLength();i++)
    {
    Element result1=(Element)results.item(i);
    String cover=result1.getAttribute("cover");
    String title=result1.getAttribute("title");
    String year=result1.getAttribute("year");
    String director=result1.getAttribute("director");
    String rating=result1.getAttribute("rating");
    String details=result1.getAttribute("details");

    jsondata+=jsondata+"\"cover:\":\""+cover+"\",";
    jsondata+=jsondata+"\"title:\":\""+title+"\",";
    jsondata+=jsondata+"\"year:\":\""+year+"\",";
    jsondata+=jsondata+"\"director:\":\""+director+"\",";
    jsondata+=jsondata+"\"rating:\":\""+rating+"\",";
    jsondata+=jsondata+"\"details:\":\""+details+"\",";





 }
out.println(jsondata);

 }catch(Exception e){out.println("problem");}


 }  
}

Add a call to the printStackTrace() method to the catch block to get more information about where and what exception is being thrown.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.