Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取服务器中的XML文件并将其显示给客户端(从服务器)_Java_Xml_Client_Server - Fatal编程技术网

Java 读取服务器中的XML文件并将其显示给客户端(从服务器)

Java 读取服务器中的XML文件并将其显示给客户端(从服务器),java,xml,client,server,Java,Xml,Client,Server,我在这里要完成的是读取XML文件并将其显示给客户机。我已经使用命令提示符运行了Server.java和Client.java。如果客户机输入选项2,则客户机将看到电影列表。但无法显示它 客户 import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; impor

我在这里要完成的是读取XML文件并将其显示给客户机。我已经使用命令提示符运行了Server.java和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文件名MovieList

<?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>