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");}
}
}
gaurangj 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.