我在这里要完成的是从XML文件中读取并将其显示给客户端。我已经运行了Server.java和amp; Client.java使用命令提示符。如果客户端输入选择号2,则客户端会想要查看电影列表。但是无法显示它。
客户
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import java.util.Scanner;
import org.w3c.dom.Node;
import java.io.File;
import java.util.*;
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String args[]) throws Exception
{
Scanner userInput = new Scanner(System.in);
Socket sockCL = new Socket("localhost", 619);
System.out.println("Successfully connected to server..");
System.out.println();
System.out.println("===========================");
System.out.println(" Movie BoxOffice Database");
System.out.println("===========================");
System.out.println();
try
{
File movielist = new File("F:\\Study Stuff\\DC\\Assignment Vol 2\\A2\\MovieList.xml");
DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbFac.newDocumentBuilder();
Document doc = db.parse(movielist);
NodeList nl = doc.getElementsByTagName("movie");
System.out.println();
System.out.println("*************************");
System.out.println("Movie List");
System.out.println("*************************");
System.out.println();
for (int x = 0;x < nl.getLength();x++)
{
Node noder = nl.item(x);
if (noder.getNodeType() == Node.ELEMENT_NODE)
{
Element Elem = (Element) noder;
System.out.println("Movie ID : " + Elem.getAttribute("id"));
System.out.println("Title : " + Elem.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Genre : " + Elem.getElementsByTagName("genre").item(0).getTextContent());
System.out.println("Duration : " + Elem.getElementsByTagName("duration").item(0).getTextContent());
System.out.println();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("(1) Search Movie");
System.out.println("(2) View Move List");
System.out.println("(3) Add New Movie");
System.out.println("(4) Exit");
System.out.println("Enter your option, either 1, 2,3 or 4.");
DataOutputStream toServer
= new DataOutputStream(sockCL.getOutputStream());
BufferedReader serverResp
= new BufferedReader(new InputStreamReader(sockCL.getInputStream()));
int op = userInput.nextInt();
toServer.writeInt(op);
String reply = serverResp.readLine();
System.out.println("SERVER RESPONSE: " + reply + "\n");
}
}
服务器
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import java.util.Scanner;
import org.w3c.dom.Element;
import java.io.File;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket infilSocket = new ServerSocket(619);
System.out.println("Loading......\n");
while(true)
{
Socket connSocket = infilSocket.accept();
System.out.println("Successfully connecting to server..\n");
DataInputStream clInput = new DataInputStream(connSocket.getInputStream());
int opt = clInput.readInt();
System.out.println(opt);
while (opt != 4)
{
try
{
switch(opt)
{
case 2 :
try {
File movielist = new File("F:\\Study Stuff\\DC\\Assignment Vol 2\\A2\\MovieList.xml");
DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbFac.newDocumentBuilder();
Document doc = db.parse(movielist);
//doc.getDocumentElement().normalize();
NodeList nl = doc.getElementsByTagName("movie");
DataOutputStream toCL = new DataOutputStream(connSocket.getOutputStream());
String h1 = "*************************";
String header = "Movie List";
toCL.writeBytes(h1);
toCL.writeBytes(header);
toCL.writeBytes(h1);
for (int x = 0;x < nl.getLength();x++)
{
Node noder = nl.item(x);
if (noder.getNodeType() == Node.ELEMENT_NODE)
{
Element Elem = (Element) noder;
toCL.writeBytes("Movie ID : " + Elem.getAttribute("id"));
toCL.writeBytes("Title : " + Elem.getElementsByTagName("title").item(0).getTextContent());
toCL.writeBytes("Genre : " + Elem.getElementsByTagName("genre").item(0).getTextContent());
toCL.writeBytes("Duration : " + Elem.getElementsByTagName("duration").item(0).getTextContent());
System.out.println();
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
break;
}
}
catch (Exception e)
{
System.out.println("Invalid input ! Please try again.");
}
}
}
}
}
XML文件名&#34; MovieList&#34;
<?xml version="1.0"?>
<movies>
<movie id="1001">
<title>Blackhat</title>
<genre>thriller</genre>
<duration>90 mins </duration>
</movie>
<movie id="1002">
<title>The Wedding Ringer</title>
<genre>comedy</genre>
<duration>100 mins </duration>
</movie>
<movie id="1003">
<title>The Avengers</title>
<genre>action</genre>
<duration>180 mins </duration>
</movie>
<movie id="1004">
<title>Taken 3</title>
<genre>action</genre>
<duration>100 mins </duration>
</movie>
<movie id="1005">
<title>Insurgent</title>
<genre>Science Fiction</genre>
<duration>180 mins</duration>
</movie>
<movie id="1006">
<title>Jurassic World</title>
<genre>Adventure</genre>
<duration>120 mins</duration>
</movie>
<movie id="1007">
<title>Interstellar</title>
<genre>Science Fiction</genre>
<duration>180 mins</duration>
</movie>
</movies>