Answers.Body: <p>After reading a bunch of open source code, I just found out that most of them are relying on <em>Chesspresso</em> which is a solid Java Chess library that can handle move validation, PGN parser (what I was looking for), chessboard renderering, etc.</p>
  
  <p>The code is well-documented and easy to understand. It took me around 30 minutes to read the code and start testing. Pretty happy with this library.</p>
  
  <p>You can download the source code from sourceforge: <a href="http://sourceforge.net/projects/chesspresso/files/?source=navbar" rel="noreferrer">Chesspresso Source Code</a></p>
  
  <p><strong>Sample code:</strong> (This is a fragment from Game class, I just need to add more detail for the header section and I will be ready to dump each pgn game into my database).</p>
  
  <pre><code>    public String getHeaderString(int line)
  {
      if (line == 0) {
          StringBuffer sb = new StringBuffer();
          sb.append(getWhite());
          if (getWhiteElo() != 0) sb.append(" [").append(getWhiteElo()).append("]");
          sb.append(" - ").append(getBlack());
          if (getBlackElo() != 0) sb.append(" [").append(getBlackElo()).append("]");
          sb.append("  ").append(getResultStr()).append("  (").append(getNumOfMoves()).append(")");
          return sb.toString();
      } else if (line == 1) {
          StringBuffer sb = new StringBuffer();
          sb.append(getEvent()).append(", ").append(getSite()).append(", ").append(getDate());
          sb.append("  [").append(getRound()).append("]");
          return sb.toString();
      } else if (line == 2) {
          return getECO();
      } else {
          throw new RuntimeException("Only 3 header lines supported");
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-12T14:02:46.397Z
Answers.LastActivityDate: 2015-10-12T14:02:46.397Z
Answers.UserDisplayName: Chale CS
Answers.Body: <p>You could use <a href="https://github.com/bhlangonijr/chesslib" rel="nofollow noreferrer" title="chesslib">chesslib</a>.</p>
  
  <p>In the <a href="https://github.com/bhlangonijr/chesslib/blob/master/README.md" rel="nofollow noreferrer">README</a> file there is an example of how to load and walk through all the moves from all the games in the PGN file: </p>
  
  <pre><code>    PgnHolder pgn = new PgnHolder("/opt/games/linares_2002.pgn");
      pgn.loadPgn();
      for (Game game: pgn.getGame()) {
          game.loadMoveText();
          MoveList moves = game.getHalfMoves();
          Board board = new Board();
          //Replay all the moves from the game and print the final position in FEN format
          for (Move move: moves) {
              board.doMove(move);
          }
          System.out.println("FEN: " + board.getFen());
      }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-16T12:29:33.760Z
Answers.LastActivityDate: 2018-06-16T12:29:33.760Z
Answers.UserDisplayName: Ben-Hur Langoni Junior
Answers.Body: <p>As a general hint: Look at Open Source Android chess games. Like DroidFish (<a href="https://github.com/peterosterlund2/droidfish" rel="nofollow">https://github.com/peterosterlund2/droidfish</a>) Maybe you can find there what you are looking for or at least use it as a starting point.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-07T12:07:45.343Z
Answers.LastActivityDate: 2015-10-07T12:07:45.343Z
Answers.UserDisplayName: Peter M
Answers.Body: <p>I know I am late to the party but if anyone is still looking for answer then here's complete source code of my <a href="https://kgcorner.com/chess-pgn-parser-in-java/" rel="nofollow noreferrer">png parser</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-23T11:49:47.953Z
Answers.LastActivityDate: 2021-07-23T11:49:47.953Z
Answers.UserDisplayName: Kumar Gaurav
Answers.Body: <p>I created this <a href="https://github.com/lunalobos/chessapi4j" rel="nofollow noreferrer">library</a> for chess-related operations. It includes a PGN parser.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-03T16:29:40.527Z
Answers.LastActivityDate: 2024-09-03T16:29:40.527Z
Answers.UserDisplayName: Miguel
Body: <p>I would like to find a Java library that will allow me to read a PGN file that could contain several chess games, probably with comments inserted also. But, it won't be used to generate moves or play the moves in a board. I just want to read the PGN and get the header section and move section, and dump everything into a database.</p>
  <p>Any light that will direct me to the right path is really appreciated. I just need the parser and segment the information from the PGN and then I will do the dumping into the database.</p>
  
Tags: <database><pgn><master-games><annotation>
Title: Java Library for PGN parser
ThreadText: # Java Library for PGN parser
  
  (Asked by: username_0 on 2015-10-01)
  
  I would like to find a Java library that will allow me to read a PGN file that could contain several chess games, probably with comments inserted also. But, it won't be used to generate moves or play the moves in a board. I just want to read the PGN and get the header section and move section, and dump everything into a database.
  
  Any light that will direct me to the right path is really appreciated. I just need the parser and segment the information from the PGN and then I will do the dumping into the database.
  
  **Tags:** database, pgn, master-games, annotation
  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_0 on 2015-10-12. Score: 10
  
  After reading a bunch of open source code, I just found out that most of them are relying on *Chesspresso* which is a solid Java Chess library that can handle move validation, PGN parser (what I was looking for), chessboard renderering, etc.
  
  The code is well-documented and easy to understand. It took me around 30 minutes to read the code and start testing. Pretty happy with this library.
  
  You can download the source code from sourceforge: [Chesspresso Source Code](http://sourceforge.net/projects/chesspresso/files/?source=navbar)
  
  **Sample code:** (This is a fragment from Game class, I just need to add more detail for the header section and I will be ready to dump each pgn game into my database).
  
  ```
      public String getHeaderString(int line)
  {
      if (line == 0) {
          StringBuffer sb = new StringBuffer();
          sb.append(getWhite());
          if (getWhiteElo() != 0) sb.append(" [").append(getWhiteElo()).append("]");
          sb.append(" - ").append(getBlack());
          if (getBlackElo() != 0) sb.append(" [").append(getBlackElo()).append("]");
          sb.append("  ").append(getResultStr()).append("  (").append(getNumOfMoves()).append(")");
          return sb.toString();
      } else if (line == 1) {
          StringBuffer sb = new StringBuffer();
          sb.append(getEvent()).append(", ").append(getSite()).append(", ").append(getDate());
          sb.append("  [").append(getRound()).append("]");
          return sb.toString();
      } else if (line == 2) {
          return getECO();
      } else {
          throw new RuntimeException("Only 3 header lines supported");
      }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2018-06-16. Score: 4
  
  You could use [chesslib](https://github.com/bhlangonijr/chesslib "chesslib").
  
  In the [README](https://github.com/bhlangonijr/chesslib/blob/master/README.md) file there is an example of how to load and walk through all the moves from all the games in the PGN file:
  
  ```
      PgnHolder pgn = new PgnHolder("/opt/games/linares_2002.pgn");
      pgn.loadPgn();
      for (Game game: pgn.getGame()) {
          game.loadMoveText();
          MoveList moves = game.getHalfMoves();
          Board board = new Board();
          //Replay all the moves from the game and print the final position in FEN format
          for (Move move: moves) {
              board.doMove(move);
          }
          System.out.println("FEN: " + board.getFen());
      }
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2015-10-07. Score: 2
  
  As a general hint: Look at Open Source Android chess games. Like DroidFish (<https://github.com/peterosterlund2/droidfish>) Maybe you can find there what you are looking for or at least use it as a starting point.
  
  
  ---
  
  ### Answer by username_3 on 2021-07-23. Score: 2
  
  I know I am late to the party but if anyone is still looking for answer then here's complete source code of my [png parser](https://kgcorner.com/chess-pgn-parser-in-java/)
  
  
  ---
  
  ### Answer by username_4 on 2024-09-03. Score: 0
  
  I created this [library](https://github.com/lunalobos/chessapi4j) for chess-related operations. It includes a PGN parser.
  
  
  ---
  
  