Using MPXJ Library in Java

Introduction 

 
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: 
    1. import net.sf.mpxj;  
    2. import net.sf.mpxj.reader;  
    3. import net.sf.mpxj.writer;  
    4. import net.sf.mpxj.mpp; 
  3. Create an object of the ProjectReader and ProjectFile classes, as in:
    1. ProjectReader reader = new MPPReader();  
    2. ProjectFile projectObj = reader.read();
    • Tasks
      1. for (Task task : project.getAllTasks())  
      2. {  
      3.    System.out.println("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID());  
      4. } 
    • Resources
      1. for (Resource resource : project.getAllResources())  
      2. {  
      3.   System.out.println("Resource: " + resource.getName() + " (Unique ID=" + resource.getUniqueID() + ")");  
      4. } 
    • SubTasks
      1. List tasks = project.getChildTasks();  
      2. Task task = tasks.get(0);  
      3. tasks = task.getChildTasks(); 
    • Access Resource and Task  by ID
      1. Resource r = project.getResourceByUniqueID(Integer.valueOf(99));  
      2. Task t = project.getTaskByUniqueID(Integer.valueOf(99)); 
    • Calendar
      1. ProjectCalendar defaultCalendar = projectObj.getCalendar();           
      2. ProjectCalendar taskCalendar = task.getCalendar(); 
    • Predecessor relationships between tasks
      1. for (Task task: file.getAllTasks())  
      2. {  
      3.     List predecessors = task.getPredecessors();  
      4.     if (predecessors != null && predecessors.isEmpty() == false)  
      5.     {  
      6.         System.out.println(task.getName() + " predecessors:");  
      7.         for (Relation relation: predecessors)  
      8.         {  
      9.             System.out.println("   Task: " + file.getTaskByUniqueID(relation.getTaskUniqueID()).getName());  
      10.             System.out.println("   Type: " + relation.getType());  
      11.             System.out.println("   Lag: " + relation.getDuration());  
      12.         }  
      13.     }  
      14. } 
  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 an MPP file then use the following:
     
    • Create Source ProjectFile Object 
      1. ProjectReader reader = new MPPReader();  
      2. ProjectFile sourceObj = reader.read(); 
    • Create object of projectWriter class
      1. ProjectWriter writer = ProjectWriterUtility.getProjectWriter();  
      2. writer.write(sourceObj, );
Reference
 


Similar Articles