Simple XML Checker Objective Write a program that checks whe

Simple XML Checker!

Objective:

Write a program that checks whether or not a given file is formatted correctly for a very simple version of XML. In this simple version of XML you have tags that denote information. Each tag has a “start-tag”, and an “end-tag”. The start-tag is denoted by a value enclosed by “< >”, and the end-tag similarly denoted by “”. Each start-tag must have an end-tag. Elements fall in between the start and end tags, and can also other tags can be nested in as well. In this version of XML you can assume that tags and elements will always be on separate lines, and no additional attributes (such as id = “3”>) will be a part.

Well formatted example

            33

Another well formatted example

           

                        100

           

Not well formatted example

            44

Another not well formatted example

           

                        33

To solve this problem you must:

Write a stack, and use it to solve this problem

You may NOT use the built-in java stack

You may either implement the stack as a linked structure or an array. If you use an array assume the max size is 100;

Write another class that has a main method which takes in a file name and checks whether or not that file is correctly formatted.

Here’s a basic idea:

If the next line is enclosed by “< >” with a tag in between, then push the tag onto the stack

If the next line is enclosed by “” with a tag in between, then

Pop one element off the stack

Check if that element matches with end-tag. If it does then continue on, but if it doesn’t then it is not formatted correctly

All other values and elements can be ignored

If by the end there are no tags left on the stack then it is properly formatted, but if the stack still has tags then it is not properly formatted.

Also the string methods “charAt(index)” and “substring(startIndex, endIndex)” may be very useful

Here are some files to test with

goodXML1

goodXML2

goodXML3

badXML1

badXML2

badXML3

Example Dialog:

Welcome to the simple XML tester. Time to test simple XML\'s

Enter a file name

goodXML1.txt

This XML File was formatted correctly

Another Example Dialog:

Welcome to the simple XML tester. Time to test simple XML\'s

Enter a file name

badXML1.txt

This XML file is not formatted correctly

The tag \"bad\" does not have a match

Yet Another Example Dialog:

Welcome to the simple XML tester. Time to test simple XML\'s

Enter a file name

badXML3.txt

This XML File was not formatted correctly

The following tags didn\'t have an end tag

super really bad

really bad

bad

Solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class XMLParser {
  
   /**
   * Opens and reads the given file and prints whether it has valid
   * XML content or not
   *
   * @param filePath
   */
   public void parseFile(String filePath) {
       String inputText = new String();
       try {
           inputText = getText(filePath);
       } catch (IOException e) {
           e.printStackTrace();
       }
      
       if (isValidXML(inputText)) {
           System.out.println(\"The given file is a valid XML file\");
           return;
       }
   }
  
   /**
   * Reads the complete file and retuns it as a string
   *
   * @param filePath
   * @return
   * @throws IOException
   */
   public String getText(String filePath) throws IOException {
       String inputLine;
       StringBuffer buffer = new StringBuffer();
       BufferedReader inputReader = new BufferedReader(new FileReader(filePath));
      
       while ((inputLine = inputReader.readLine()) != null) {
           buffer.append(inputLine+\"\ \");
       }
      
       inputReader.close();
       return buffer.toString();
   }
  
   /**
   * Retuns whether the given XML text is a valid or not
   * @param inputText
   * @return
   */
   public boolean isValidXML(String inputText) {
       UserStack stack = new UserStack();

       int i=0;
       while (i<inputText.length()) {
          
           if (inputText.charAt(i) == \'<\') {
              
               /* Find out whether it is a start tag or end tag
               */
               boolean isStartTag = true;
               if (inputText.charAt(i+1) == \'/\') {
                   isStartTag = false;
                   i++;
               }
               int j = i+1;
              
               /* Read the tag name
               */
               StringBuffer tag = new StringBuffer();
               while (inputText.charAt(j) != \'>\') {
                   tag.append(inputText.charAt(j));
                   j++;
               }
               i=j+1;
              
               /* 1. push on the top of the stack, if it is a start tag
               * 2. Otherwise pop an element from the stack and compare
               */
               if (isStartTag) {
                   stack.push(tag.toString());
               }
               else {
                   String tos = stack.peek();
                   if (tos == null) {
                       System.out.println(\"The given file is not a valid XML file\ \");
                       return false;
                   }
                  
                   if (tos.equals(tag.toString())) {
                       stack.pop();
                   }
                   else {
                       System.out.println(\"The given file is not a valid XML file\ \"
                               + \"Following elements didn\'t had a tag : \");
                       stack.printStack();
                       return false;
                   }
               }
           }
           else {
               i++;
           }
       }
      
       /*
       * If the stack contains elements at the end
       * then XML is invalid
       */
       if (!stack.isEmpty()) {
           System.out.println(\"The given file is not a valid XML file\ \"
                   + \"Following elements didn\'t had a tag : \");
           stack.printStack();
           return false;
       }
       return true;
   }
  
   public static void main(String args[]) {
      
       /* Take in the file name from the user
       * */
       Scanner inputScanner = new Scanner(System.in);
       System.out.println(\"Enter a file name : \");
       String fileName = \"/home/zubair/Desktop/\" + inputScanner.nextLine();
       inputScanner.close();

       /*
       * Validate the content of the file
       * */
       XMLParser parser = new XMLParser();
       parser.parseFile(fileName);
   }
}


class UserStack {
  
   private static final int MAX_CAPACITY = 100;
   private String array[] = new String[MAX_CAPACITY];  
  
   private int top = -1;

   public void push(String element) {
       top++;
       if (top < MAX_CAPACITY) {
           array[top] = element;
           return;
       }
       System.out.println(\"Stack is full, please remove some items\");
   }

   public String pop() {
       if (top < 0) {
           System.out.println(\"Stack is empty\");
           return null;
       }
       return array[top--];
   }

   public String peek() {
       if (top < 0) {
           System.out.println(\"Stack is empty\");
           return null;
       }
       return array[top];
   }
  
   public boolean isEmpty() {
       if (top < 0) {
           return true;
       }
       return false;
   }
  
   public int size() {
       return (top + 1);  
   }
  
   public void printStack() {
       for (int i=0; i<=top; i++) {
           System.out.println(array[i]);
       }
       System.out.println();
   }
}

Simple XML Checker! Objective: Write a program that checks whether or not a given file is formatted correctly for a very simple version of XML. In this simple v
Simple XML Checker! Objective: Write a program that checks whether or not a given file is formatted correctly for a very simple version of XML. In this simple v
Simple XML Checker! Objective: Write a program that checks whether or not a given file is formatted correctly for a very simple version of XML. In this simple v
Simple XML Checker! Objective: Write a program that checks whether or not a given file is formatted correctly for a very simple version of XML. In this simple v
Simple XML Checker! Objective: Write a program that checks whether or not a given file is formatted correctly for a very simple version of XML. In this simple v

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site