![]() |
| XML parsed and displayed as a table |
Write a Program to read an xml file stored in the server. A client connected to the server gets the file and display the contents in a table using swing. The xml file contains details of books with the following fields.
- Title of the book
- Author of book
- Publisher of book
- Price of book
- Price of book
- Number of pages in the book
A sample xml file:
<BOOKS> <BOOK> <TITLE>Time Machine</TITLE> <AUTHOR>H.G Wells</AUTHOR> <PUBLISHER>William Heinemann</PUBLISHER> <PRICE> 100 Rs</PRICE> <PAGES> 500 </PAGES> </BOOK> <BOOK> <TITLE>Swami and His friends</TITLE> <AUTHOR>RK narayanan</AUTHOR> <PUBLISHER>Hamilton</PUBLISHER> <PRICE> 250 Rs</PRICE> <PAGES> 100 </PAGES> </BOOK> <BOOK> <TITLE>Harry potter</TITLE> <AUTHOR>J K Rowling</AUTHOR> <PUBLISHER>Bloomsbury</PUBLISHER> <PRICE> 600 Rs</PRICE> <PAGES> 800 </PAGES> </BOOK> </BOOKS>
The java program to send xml files of above format over network and display it as a table at client side is as follows:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
public class XMLTable extends JFrame{
JLabel lbl=new JLabel();
ServerSocket ss;
Socket s;
ArrayList<Book> books;
boolean server;
File xml;
String received=null;
public XMLTable(boolean mod)
{
try{
server=mod;
setLayout(null);
setSize(400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(lbl);
lbl.setBounds(10,10,370,370);
if(server)
{
JFileChooser jfc=new JFileChooser();
jfc.setFileFilter(new FileNameExtensionFilter("xml file","xml"));
jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
jfc.setDialogTitle("Select xml file");
if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
xml=jfc.getSelectedFile();
}
else
System.exit(0);
lbl.setText("<HTML><font size=\"4\">File Selected.<br/>Waiting for connection..<br/>IP:"+InetAddress.getLocalHost().getHostAddress()+"</font></html>");
setVisible(true);
ss=new ServerSocket(33333);
ss.setSoTimeout(0);
s=ss.accept();
thread.start();
}
else
{
String ip=JOptionPane.showInputDialog("Enter IP of server.");
lbl.setText("<HTML><font size=\"4\">Trying to connect..</font></html>");
setVisible(true);
s=new Socket(InetAddress.getByName(ip),33333);
thread.start();
}
}catch(Exception ex){ex.printStackTrace();}
}
public void CreateTable(String received)
{
String temp=received;
try{
int i=0,j;
books=new ArrayList<>();
while((i=temp.indexOf("<BOOK>",i))!=-1)
{
Book b=new Book();
i=temp.indexOf("<TITLE>",i)+7;
j=temp.indexOf("</TITLE>",i);
b.title=temp.substring(i,j);
i=j+8;
i=temp.indexOf("<AUTHOR>",i)+8;
j=temp.indexOf("</AUTHOR>",i);
b.author=temp.substring(i,j);
i=j+9;
i=temp.indexOf("<PUBLISHER>",i)+11;
j=temp.indexOf("</PUBLISHER>",i);
b.publisher=temp.substring(i,j);
i=j+12;
i=temp.indexOf("<PRICE>",i)+7;
j=temp.indexOf("</PRICE>",i);
b.price=temp.substring(i,j);
i=j+8;
i=temp.indexOf("<PAGES>",i)+7;
j=temp.indexOf("</PAGES>",i);
b.pages=temp.substring(i,j);
i=j+8;
i=temp.indexOf("</BOOK>",i)+7;
books.add(b);
System.out.println("book:"+b);
}
String htmlcode="<HTML><table border=\"1\"><tr><th>Title</th><th>Author</th><th>Publisher</th><th>Price</th><th>Pages</th></tr>";
for(i=0;i<books.size();i++)
{
htmlcode+="<tr><td>"+books.get(i).title+"</td><td>"+books.get(i).author+"</td><td>"+books.get(i).publisher+"</td><td>";
htmlcode+=books.get(i).price+"</td><td>"+books.get(i).pages+"</td></tr>";
}
htmlcode+="</table></html>";
lbl.setText(htmlcode);
}catch(Exception ex){ex.printStackTrace();}
}
public static void main(String args[])
{
int resp=JOptionPane.showOptionDialog(null,"Choose one of the options","What to dd?",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, null,new String[]{"Act as Server","Act as Client"},"Act as Server");
if(resp==0)
new XMLTable(true);
else if(resp==1)
new XMLTable(false);
}
Thread thread=new Thread()
{
public void run()
{
try{
BufferedReader br;
if(server)
{
lbl.setText("<HTML><font size=\"4\">Connected.<br/>Sending XML File.</font></html>");
br=new BufferedReader(new FileReader(xml));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String l=null;
while(true)
{
l=br.readLine();
if(l==null||l.equals(""))
break;
pw.println(l);
}
System.out.println("out of loop");
ss.close();
s.close();
}
else
{
lbl.setText("<HTML><font size=\"4\">Connected.<br/>Receiving xml</font></html>");
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String l="";
s.setSoTimeout(1500);
while((l=br.readLine())!=null)
{
received+=l+"\n";
System.out.println(l);
}
System.out.println("out of loop");
if(received.equals("")==false)
CreateTable(received);
else throw new Exception("Connection interrupted");
}
}catch(Exception ex){ex.printStackTrace();}
}
};
}
class Book
{
public String title;
public String author;
public String publisher;
public String price;
public String pages;
}

No comments:
Post a Comment