Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 29th, 2005, 06:39 AM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to send an XML document to a server?

Here's what i've been trying to achieve for about a week now:

I'm trying to develop a simple search engine the results of searches are stored in an xml repository the only way to access the server is through TCP/IP sockets it accepts an input (xml query) and returns another xml as output with the answers.

I have to create an xml document (using dom) based on the users search (done that)
then after that i should be able to send the XML file created to the server via TCP/IP sockets (i've partially completed this
basically my code opens an input and output stream and it works. However when i send a query as a string it just returns blank results, meaning the query is in the wrong format. I have to send the XML file.

Next, when i recieve the response from the server with the answers (assuming that the query is in the right format) i'll get back a list of answers in XML format. Now what i want is to display this in a html file so the user sees it.

Any help would be appreciated.

Code:
import java.net.*;
import java.io.*;
 
public class SendQuery {
 
  public static void main (String[] args) throws Exception
     {
 
    try {
        // Construct data
        String data = "<?xml version=\"1.0\"?>";
        data += "<query />";
        System.out.println(data);

        // Create a socket to the host
        String hostname = "testserver:313";
        int port = 313;
        InetAddress addr = InetAddress.getByName(hostname);
        Socket socket = new Socket(addr, port);

        // Send header
        String path = "/servlet/SomeServlet";
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
        wr.write("hello");

        // Send data
        wr.write(data);
        wr.flush();

        // Get response
        BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();
    } catch (Exception e) {
    }

        try {
        // Create a URL for the desired page
        URL url = new URL("http://testserver:313");
 

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
            System.out.println(str);
        }
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
}
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
XMl Document Reading in SQL Server shashideore SQL Server 2005 1 April 26th, 2007 07:16 AM
how to send xml file from server to client samarjit_mishra XML 1 January 27th, 2007 09:22 AM
Write Nested XML document into SQL Server tables boondocksaint20 VB.NET 0 May 2nd, 2006 12:35 PM
send XML Request and get XML Response prashant_dnmmkpk VB.NET 2 February 25th, 2005 03:34 PM
Send XML document to a webservice vinodveladanda XML 1 November 11th, 2003 11:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.