-2

Now I use the sax parser to analysis the xml file(the following sample code comes from tutorialspoint). I want to output all the things to a txt file using the BufferedWriter when we call the characters function, while I don't get a way to make this. If I write this BufferedWriter in the characters function, it will only output the last string, since it will only record the last call. If I write this BufferedWriter in the main function, since they are two classes, the UserHandler couldn't recognize the BufferedWriter created in the main function(the dblpSax class). I also tried sth to make a class instance, while I still got involved troubles. Could you give me a way to make this? Thanks.

The Bufferwriter I want to use is as follows:

File writename = new File("output.txt");
writename.createNewFile(); 
BufferedWriter out = new BufferedWriter(new FileWriter(writename));



    public class dblpSax{
   public static void main(String[] args){

      try { 
         File inputFile = new File("student.txt");
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         UserHandler userhandler = new UserHandler();
         saxParser.parse(inputFile, userhandler);     
      } catch (Exception e) {
         e.printStackTrace();
      }
   }   
}


class UserHandler extends DefaultHandler {

   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;


   public void startElement(String uri, 
      String localName, String qName, Attributes attributes)
         throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         String rollNo = attributes.getValue("rollno");
         System.out.println("Roll No : " + rollNo);
      } else if (qName.equalsIgnoreCase("firstname")) {
         bFirstName = true;
      } else if (qName.equalsIgnoreCase("lastname")) {
         bLastName = true;
      } else if (qName.equalsIgnoreCase("nickname")) {
         bNickName = true;
      }
      else if (qName.equalsIgnoreCase("marks")) {
         bMarks = true;
      }
   }


   public void endElement(String uri, 
      String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         System.out.println("End Element :" + qName);
      }
   }


   public void characters(char ch[], 
      int start, int length) throws SAXException {
      if (bFirstName) {
         System.out.println("First Name: " 
         + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName) {
         System.out.println("Last Name: " 
         + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName) {
         System.out.println("Nick Name: " 
         + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks) {
         System.out.println("Marks: " 
         + new String(ch, start, length));
         bMarks = false;
      }
   }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
michael
  • 39
  • 7
  • And the problem is what? NB The `createNewFile()` call is redundant above. – user207421 Jul 19 '16 at 05:45
  • I want to output all the things to a .txt file using the BufferedWriter when we call the characters function, the question is: how to use the BufferedWriter? – michael Jul 19 '16 at 05:48

1 Answers1

0

You need a BufferedWriter at the same level as the parsing, best customizable outside the DefaultHandler.

        try (BufferedWriter out = new BufferedWriter(....)) {
            UserHandler userhandler = new UserHandler(out);
            saxParser.parse(inputFile, userhandler);     
        }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }   
}

class UserHandler extends DefaultHandler { private final BufferedWriter out; // Or PrintWriter

UserHandler(BufferedWriter out) {
    this.out = out;
}

And then one can all out.write everywhere.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks Joop. This really helps and I learn how to interact between the classes with a construction method. Thanks for your nice even someone gave minus mark! (while I think they don't consider carefully lol) – michael Jul 27 '16 at 07:40