hi,
i was able to append a task to an mpx file but the task was not visible in the MS Project 2003 while opening, i,e Through program if i read the newly added task, i can able to read the new Task. But i was successful with microsoft xml format that is MSPDI.the code which was used is,
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import net.sf.mpxj.*;
import net.sf.mpxj.mpp.MPPReader;
import net.sf.mpxj.mpx.MPXReader;
import net.sf.mpxj.mpx.MPXWriter;
import net.sf.mpxj.mspdi.*;
import net.sf.mpxj.mspdi.MSPDIWriter;
import net.sf.mpxj.utility.NumberUtility;
import net.sf.mpxj.writer.ProjectWriter;
public class CreateXML
{
public CreateXML ()
{
}
public static void main(
String args[])
{
try
{
create("d:\\output.XML");
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
}
}
private static ProjectWriter getWriter(String filename)
{
String suffix;
if(filename.length() < 4)
suffix = ".MPX";
else
suffix = filename.substring(filename.length() - 4).toUpperCase();
ProjectWriter result;
if(suffix.equals(".XML"))
result = new MSPDIWriter();
else
result = new MPXWriter();
return result;
}
private static void create(String filename)
throws Exception
{
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
MSPDIReader r=new MSPDIReader();
ProjectFile file=r.read("d:\\input.XML");
file.setAutoTaskID(true);
file.setAutoTaskUniqueID(true);
file.setAutoResourceID(true);
file.setAutoResourceUniqueID(true);
file.setAutoOutlineLevel(true);
file.setAutoOutlineNumber(true);
file.setAutoWBS(true);
file.setAutoCalendarUniqueID(true);
ProjectCalendar calendar = file.addDefaultBaseCalendar();
ProjectCalendarException exception = calendar.addCalendarException();
exception.setFromDate(df.parse("13/06/2007"));
exception.setToDate(df.parse("13/06/2007"));
exception.setWorking(false);
ProjectHeader header = file.getProjectHeader();
header.setStartDate(df.parse("1/06/2007"));
Resource resource1 = file.addResource();
resource1.setName("Resource1");
Resource resource2 = file.addResource();
resource2.setName("Resource2");
resource2.setMaxUnits(new Double(50D));
Task task1 = file.addTask();
task1.setName("Main Task2");
task1.setStart(df.parse("12/07/2007"));
task1.setDuration(Duration.getInstance(100D, TimeUnit.DAYS));
Task task2 = task1.addTask();
task2.setName("T13");
task2.setDuration(Duration.getInstance(5, TimeUnit.DAYS));
task2.setStart(df.parse("6/10/2007"));
task2.setPercentageComplete(NumberUtility.getDouble(50D));
ProjectWriter writer = getWriter(filename);
writer.write(file, filename);
}
}
But with the above code, The new task is not appending in the proper Order..