SENDING XML DATA ?

Hello,

For a project i need to create a java application that can send a xml file to a client that is able to read it.

I have created the java application and that works fine, for example i can send some text from 1 laptop to another laptop.

Now I need some help to send xml file from the server to a client. Unfortuantly iam not so good with XML. How can I adjust the code , so i can send xml data from 1 laptop to the other laptop? Thanks in advance.

The JAVA CODE FOR THE SERVER:

package remoteControl;

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class RemoteControl {

JTextArea incoming;

BufferedReader reader;

PrintWriter writer;

Socket sock;

public static void main(String[] args) {

RemoteControl remote = new RemoteControl();

remote.go();

}

public void go(){

JFrame frame = new JFrame("Remote Control");

JPanel mainPanel = new JPanel();

incoming = new JTextArea(20,30);

incoming.setLineWrap(true);

incoming.setWrapStyleWord(true);

incoming.setEditable(true);

JScrollPane qScroller = new JScrollPane(incoming);

qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

JButton sendButton = new JButton("Send");

JButton sendButton2 = new JButton("Send2");

sendButton.addActionListener(new SendButtonListener());

sendButton2.addActionListener(new SendButton2Listener());

mainPanel.add(qScroller);

mainPanel.add(sendButton);

mainPanel.add(sendButton2);

setUpNetworking();

Thread readerThread = new Thread(new IncomingReader());

readerThread.start();

frame.getContentPane().add(BorderLayout.CENTER, mainPanel);

frame.setSize(400, 500);

frame.setVisible(true);

}

private void setUpNetworking(){

try{

sock = new Socket("127.0.0.1", 5001);

InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());

reader = new BufferedReader(streamReader);

writer = new PrintWriter(sock.getOutputStream());

System.out.println("networking established");

}catch(IOException ex){

ex.printStackTrace();

}

}

public class SendButtonListener implements ActionListener {

public void actionPerformed(ActionEvent ev){

try{

writer.println("SendButton");

writer.flush();

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public class SendButton2Listener implements ActionListener {

public void actionPerformed(ActionEvent ev){

try{

writer.println("SendButton2");

writer.flush();

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public class IncomingReader implements Runnable{

public void run(){

String message;

try{

while((message = reader.readLine()) != null){

System.out.println("read " + message);

incoming.append(message + "\n");

}

}catch(Exception ex){

ex.printStackTrace();

}

}

}

}

and the CLIENT CODE :

package remoteControlClient;

import java.io.*;

import java.net.*;

import java.util.*;

import javax.swing.*;

import java.awt.*;

public class RemoteControlClient{

ArrayList clientOutputStreams;

JFrame frame;

JPanel mainPanel;

JTextArea textArea;

public class ClientHandler implements Runnable{

BufferedReader reader;

Socket sock;

public ClientHandler(Socket clientSocket){

try{

sock = clientSocket;

InputStreamReader isReader = new InputStreamReader(sock.getInputStream());

reader = new BufferedReader(isReader);

}catch(Exception ex){

ex.printStackTrace();

}

}

public void run(){

String message;

try{

while((message = reader.readLine()) != null){

//System.out.println("read " + message);

//tellEveryone(message);

//TODO: Comparison does not work!!!

if (message == "SendButton"){

textArea.setText("S");

}else if(message == "SendButton2"){

textArea.setText("SendButton2");

}

textArea.append(message + "\n");

}

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public static void main(String[] args) {

RemoteControlClient client = new RemoteControlClient();

client.go();

}

public void go(){

frame = new JFrame();

mainPanel = new JPanel();

textArea = new JTextArea(20,30);

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

textArea.setEditable(false);

mainPanel.add(textArea);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(BorderLayout.CENTER, mainPanel);

frame.setSize(400,400);

frame.setVisible(true);

clientOutputStreams = new ArrayList();

try{

ServerSocket serverSock = new ServerSocket(5001);

while(true){

Socket clientSocket = serverSock.accept();

PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());

clientOutputStreams.add(writer);

Thread t = new Thread(new ClientHandler(clientSocket));

t.start();

System.out.println("got a connection");

}

}catch(Exception ex){

ex.printStackTrace();

}

}

public void tellEveryone(String message){

Iterator it = clientOutputStreams.iterator();

while(it.hasNext()){

try{

PrintWriter writer = (PrintWriter) it.next();

writer.println(message);

writer.flush();

}catch(Exception ex){

ex.printStackTrace();

}

}

}

}

[5962 byte] By [Figoa] at [2007-11-27 6:49:29]
# 1

Hello,

You can do the following:

- Create your own XML Schema to specify what the message should be. For example, if you are sending a message about a person, you would want to know a few things. So, that message can look something like this:

<MyMessage>

<PersonQuery>

<Name>val_0</Name>

<Age>val_1</Age>

</PersonQuery>

</MyMessage>

So you can start out by specifying what a message is by writing up the XSD for it.

- Create classes to build and parse these XML message. Check out DocumentBuilderFactory and other related classes. If your clients need to respond to you somehow, you might even want to create a response message schema?

- Create Java classes that correspond to your XML Objects. To follow the example above, you would have something similar to this:

public class PersonQuery {

private String name;

private int age;

public PersonQuery () {}

// add getters and setters here

}

public class MyMessage {

private PersonQuery person;

public MyMessage() {}

// add getters and setters here

}

If your schema is quite large, check out XML Bean http://xmlbeans.apache.org/

Creating these classes makes the building and parsing process a touch more organized and easier to manage.

Hope this helps.

Message was edited by:

kdajani

kdajania at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Use JAX-WS Web Services.
dvohra09a at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Kdajani Thanks for your reply,

As i mentioned before iam not so good with xml/parsing etc.

I have tried to insert the xml code, but somehow iam doing something wrong cause its not working.

So i would appreciate it, if you could show me where to insert the xml parse code in the application code which i had placed in my first post.

I only need to send a name as Xml data, so large xml files are not needed.

Especially the parsing thing is hard to understand. I already have red so much about parsing/xml and still iam stuck.

U really would help me with this, i have only couple of days left to finish it.

Thank you very much in advance.

Figoa at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
can you repost your code with the code tags, and some comments please
kdajania at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Hi Kdajani,

You mean that the tags of the code moved to the left?

If I copy the code here , the tags will automatically move to the left.

So i have uploaded the files, u can download it here http://www.mediafire.com/?cfmbbil15cr

In the winrar file u will find 2 maps, 1 for the server and 1 map for the client.

The server code ( map: Remotecontrol under map src there is remoteControl.java) u run it (for example) on the first laptop and the Clientcode (map: RemotecontrolClient under map src there is remotecontrolclient.java) on the second laptop.

With the servercode now u can send text with a button from laptop1 and u will receive it on laptop 2 in the appearbox.

(u can ofcourse run the both codes also on 1 system )

This is how it works now, but what i need is , sending a specific xml data with a button from laptop1 to the second laptop that appears in the textbox.

And this is the code with XML , u can download it here : http://www.mediafire.com/?7np1mjfnlms

So as you can see i have tried to implement a xml code, but i get to much errors.

I hope u meant this by comment, or else let me know and i will do my best to explain it.

Hope u can help me.

Thanks in advance.

Ps if u use msn messenger , if u want i can add u for better communication.

Message was edited by:

Figo

Figoa at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
edit
Figoa at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
nobody?
Figoa at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

> So as you can see i have tried to implement a xml code, but i get to much errors.

The following code snippets define two malformed XML documents because of the "#" character:writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

writer.println("<function>Volume</function>");

writer.println("<value>10</value>#");

and:writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

writer.println("<xmlMessage>");

writer.println("<function>Opacity</function>");

writer.println("<value>30</value>");

writer.println("</xmlMessage>#");

An XML document can only contain one root element, so the "#" character should be removed.

prgguya at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Hi Prgguy,

I have removed # and tried it, but it still does not what it suppose to do.

Now when I click on the button in the Remote server application, it does not send the xml code to the client.

Also i get other errors.

See the printscreens: http://aycu37.webshots.com/image/19596/2004037607366530623_rs.jpg

And here: http://aycu19.webshots.com/image/19538/2004096650143986143_rs.jpg

Any idea what the problem now is?

Figoa at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

> I have removed # and tried it, but it still does not what it suppose to do.

> Also i get other errors.

Now that you have removed the hashmarks from your XML code, your problem is no longer XML-related. The "other errors" were presumably present all the time but were suppressed by the parsing error. I have run your code using JRE 1.6.0_01 on a standalone machine (connecting to a local port) and with the hashmarks having been removed it works fine. In theory, it should also work for you if there are no problems in your network connection. Looking at the 2004037607366530623_rs.jpg (provided that you used the same source code that you made downloadable) the stack trace reveals that the following command (in line 93) causes the error:reader.readLine()

I get a very similar error when I close the client frame:networking established

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:168)

at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)

at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)

at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)

at java.io.InputStreamReader.read(InputStreamReader.java:167)

at java.io.BufferedReader.fill(BufferedReader.java:136)

at java.io.BufferedReader.readLine(BufferedReader.java:299)

at java.io.BufferedReader.readLine(BufferedReader.java:362)

at RemoteControl$IncomingReader.run(RemoteControl.java:93)

at java.lang.Thread.run(Thread.java:619)

I looked up the code fragment surrounding the 168th line in the file java.net.SocketInputStream.java, and it has the following comment:/*

* If we get here we are at EOF, the socket has been closed,

* or the connection has been reset.

*/

I would say your problem was purely network-related, but it is in contradiction of the fact that you do not get an error when you send non-parsed text (by clicking on the <Send> button). Perhaps you should first try using other port numbers.

prgguya at 2007-7-12 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...