1 Reply Latest reply: Jan 25, 2009 4:20 PM by 800345 RSS

    Reading the XML file and displaying teh string with teh desired output

    807588
      Hi Gurus,

      I have an xml file as below.

      catalog>
      <book>
      <id>101</id>
      <genre>Computer</genre>
      <author>Jim Cortez</author>
      <title>XML for dummies</title>
      <price>44.95</price>
      <description>An in-depth look at creating mashed potatoes
      with XML.</description>
      </book>
      <book>
      <id>102</id>
      <author>George Bush</author>
      <title>I'm the decider</title>
      <genre>Fantasy</genre>
      <price>0.95</price>
      <description>I like milk and cookies.</description>
      </book>
      </catalog>


      I would like to display the Output as

      [<catalog>:1]
      [<book>:1]
      [<id>:1]
      [101:3]
      [</id>:2]
      [<genre>:1]
      [Computer:3]
      [</genre>:2]
      [<author>:1]
      [Jim Cortez:3]
      [</author>:2]
      [<title>:1]
      [XML for dummies:3]
      .................
      ............ etc., etc.,,

      here is the code template.......
      import java.io.*;

      class TagScanner implements TokenStream {
      public static final int BEGIN_TAG_TYPE = 1;
      public static final int END_TAG_TYPE = 2;
      public static final int TEXT_TYPE = 3;

      protected Reader reader = null;
      /** Lookahead char */
      protected char c;

      /** Text of currently matched token */
      protected StringBuffer text = new StringBuffer(100);

      public TagScanner(Reader reader) throws IOException {
      this.reader = reader;
      nextChar();
      }

      protected void nextChar() throws IOException {
      c = (char)reader.read();
      }

      public Token nextToken() throws IOException {
           if ( start of a tag ) {
           // scarf until end of tag
           // type is either BEGIN_TAG_TYPE or END_TAG_TYPE
           }
           if ( end of file ) {
           type = Token.EOF_TYPE;
           text = "end-of-file";
           }
           else {
           // scarf until start of a tag
           type = TEXT_TYPE;
           }
           if ( just whitespace ) {
           // ignore and get another token
           }
      return new Token(type, text.toString());
      }
      }


      Can someone please provide me the logic for the code please........... here is the complete link of the excersie http://www.antlr.org/wiki/display/CS652/Lexer+for+XML


      Many Thanks
      -M
        • 1. Re: Reading the XML file and displaying teh string with teh desired output
          800345
          Can someone please provide me the logic for the code please..........
          The logic is pretty well spelled out for you in the description of the assignment and the outline for the code you provided. If you mean
          Can someone please do my homework for me....
          The answer to that is usually yes, someone can do your homework for you, but no, they usually won't actually do it for you.

          However, if you are really stuck on what to do, you should consider:
                  if ( start of a tag ) {
          How would you know that you are at the start of a tag? Once you can answer that, the rest sort of works itself out as long as
                      // scarf until end of tag
          you realize what it means to 'scarf' and how to determine when an end-of-tag is reached (hint, very similar as to how to determine if you are at the start-of-tag).