需要程序帮助,FileInputStream只读取文本文件的一行有问题。 [HomeworkHelp]

时间:2012-03-01 07:30:02

标签: java xml fileinputstream

所以我必须做一个基本上测试XML文本文档是否正确嵌套的程序。我认为这实际上是本学期我们更简单的作业之一,但我遇到的麻烦是逐行阅读文本文件。我已经为我们的教授提供给我们的FileInputStream实现了代码,从那里我一直遇到问题。基本上,我的代码验证文本文件的第一行,然后结束。我必须对我的FileInputStream做错了,我只是不太确定是什么。任何帮助表示赞赏。

// The XMLParser class prompts the user for a filename and reads an XML 
// document from the given text file. The program then reports that either the 
// document is valid based on XML rules or there was an error at a certain 
// line of input due to a violation of rules of XML tags.

import java.io.*;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

public class XMLParser {        
public static void main(String[] args) throws IOException{

    //creates a scanner to read the users input of the file name
    Scanner input = new Scanner(System.in);

    //creates an integer that will be incremented for every line read
    //to help with error reporting
    int currentLine = 1;

    //creates a stack that elements will be pushed on to
    //and popped off of, strings to represent opening and closing
    //tags of an xml document, and a string to represent the root 
    //tag (the first tag used)
    Stack<String> stack = new Stack<String>();
    String open = "<";
    String close = "</";
    String root = "";


    //prompts the user to input a file name then reads from
    // the file the user entered
    System.out.println("Enter the name of the file to be read: ");
    String fileName = input.next();
    FileInputStream fis = new FileInputStream(fileName);
    InputStreamReader inStream = new InputStreamReader(fis);
    BufferedReader stdin = new BufferedReader(inStream);
    String data = stdin.readLine();

    String nextDataValue;
    StringTokenizer tokenizer = new StringTokenizer(data);

    while (tokenizer.hasMoreTokens()) {
        nextDataValue = tokenizer.nextToken().trim();

        //if the xml document begins with anything other than
        //a root tag, an error is reported and the program ends
        if (root.equals("") && !nextDataValue.startsWith(open)) {
            System.out.println("PARSE ERROR Line 1, your XML document does not start with an opening tag and is therefore invalid\nProgram terminating normally...");
            break;
        }

        //pushes an "opening tag" onto the stack
        else if (nextDataValue.startsWith(open) && !nextDataValue.startsWith(close)) {
            stack.push(nextDataValue);

            //checks if there is already a root value, and if there
            //is not it adds one
            if (root.equals("")) {
                root = root + nextDataValue;
            }

        }

        //if a closing tag is found, this pops the stack and compares
        //the closing tag with an opening tag to see if it is properly
        //nested, if not an error statement is printed and the program
        //ends
        if (nextDataValue.startsWith(close)) {
            String compareClose = nextDataValue;
            String compareOpen = stack.pop();
            compareClose = compareClose.replace("/", "");
            if (!compareClose.equals(compareOpen)) {
                System.out.println("PARSE ERROR Line " + currentLine + ", you have improperly nested the tag: " + compareOpen);
                break;
            }
            //if the closing tag is the root tag and there is nothing
            //after this closing tag, the document is valid.
            if (compareOpen.equals(root) && !tokenizer.hasMoreTokens()) {
                System.out.println("Input XML document is valid");
            }
        }       


        currentLine++;
    }




}
}

tl; dr:为什么我的FileInputStream通过一行代码然后停止。我在while循环中做错了什么,或者是我不知道的其他东西?

非常感谢任何帮助,谢谢!

4 个答案:

答案 0 :(得分:4)

你的问题在于这一行:String data = stdin.readLine();。您执行此操作的方式将指示程序只读取一行,验证它并退出。您需要做的是与此类似的while循环:

String data = "";
while ( (data = stdin.readLine()) != null)
{
    //Read and validate the line you are reading
}

这将允许您每次迭代加载一行新文本。一旦遇到EOFstdin.readLine()应该返回null,从而打破循环并使程序执行停止。

答案 1 :(得分:1)

就像他们说的那样,你只能从BufferedReader

中读取一行

您还需要注意空元素,例如

<emptyElement />

String open = "<";
String close = "</";
String close1 = "/>"

答案 2 :(得分:0)

您只读了一次。在循环之前。

答案 3 :(得分:0)

您必须重复调用readLine,例如在循环中,读取多行。