1

所以我必须做一个程序来测试 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;博士:为什么我的 FileInputStream 会通过一行代码然后就停止了。我在while循环中做错了什么还是我不知道的其他事情?

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

4

4 回答 4

4

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

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

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

于 2012-03-01T07:36:33.677 回答
1

就像他们说的,你只能从 BufferedReader 中读取一行

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

<emptyElement />

String open = "<";
String close = "</";
String close1 = "/>"
于 2012-03-01T07:42:55.583 回答
0

你只读了一行。循环之前。

于 2012-03-01T07:36:00.637 回答
0

您必须反复调用 readLine,例如在一个循环中,才能读取多行。

于 2012-03-01T07:36:09.940 回答