Using MPXJ Library in Java

If you want to read a Microsoft Project Plan (MPP) file in Java then you can use the MPXJ library to manipulate data of the MPP file. Using this library you can also create a new MPP file. For more information you can refer to my Article on the MPXJ Library.

To use this library you can use the following procedure:

  1. Add a reference of MPXJ.jar
  2. Import the following namespaces:

    import net.sf.mpxj;
    import net.sf.mpxj.reader;
    import net.sf.mpxj.writer;
    import net.sf.mpxj.mpp;
     
  3. Create an object of the ProjectReader and ProjectFile classes, as in:

    ProjectReader reader = new MPPReader();
    ProjectFile projectObj = reader.read();
    • Tasks
       

      for (Task task : project.getAllTasks())

      {

         System.out.println("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID());

      }

       

    •  Resources

       

      for (Resource resource : project.getAllResources())

      {

        System.out.println("Resource: " + resource.getName() + " (Unique ID=" + resource.getUniqueID() + ")");

      }
       

    • SubTasks

       List tasks = project.getChildTasks();
                  Task task = tasks.get(0);
                  tasks = task.getChildTasks();
       
    • Access Resource and Task  by ID

       Resource r = project.getResourceByUniqueID(Integer.valueOf(99));
      Task t = project.getTaskByUniqueID(Integer.valueOf(99));
       
    • Calendar

      ProjectCalendar defaultCalendar = projectObj.getCalendar();         
      ProjectCalendar taskCalendar = task.getCalendar();
       
    • Predecessor relationships between tasks

            

      for (Task task : file.getAllTasks())

      {    List predecessors = task.getPredecessors();

            if (predecessors != null && predecessors.isEmpty() == false)

             {

                  System.out.println(task.getName() + " predecessors:");

                 for (Relation relation : predecessors)

                 {

                  System.out.println("   Task: " +  file.getTaskByUniqueID(relation.getTaskUniqueID()).getName());

                  System.out.println("   Type: " + relation.getType());

                  System.out.println("   Lag: " + relation.getDuration());

                 }       

               }

        }
       

  4. Now if we want to access tasks, subtasks, calendars or resources from a MPP file then we need to use the methods using a ProjectFile Object.
  5. There are many methods provided by MPXJ library you can use depending on your requirements.
  6. Now if you want to create a MPP file then use the following:

    •  Create Source ProjectFile Object

      ProjectReader reader = new MPPReader();
      ProjectFile sourceObj = reader.read();
       
    • Create object of projectWriter class

      ProjectWriter writer = ProjectWriterUtility.getProjectWriter();
       writer.write(sourceObj, );

Reference(s)

http://mpxj.sourceforge.net/

 

Up Next
    Ebook Download
    View all
    Learn
    View all