/** * Primavera P6 XML (PMXML), the XML export of P6 Professional and EPPM. * Its root element is APIBusinessObjects; under it are the global calendars, * resources and currencies, and one Project element per project with its * WBS, Activity, Relationship, ResourceAssignment and note elements. * * The elements hold the same data as an XER file's tables under other names, * so they are turned into those tables (column names as XER has them, codes * as XER writes them) and read by the XER reader's readP6. Calendars are * read here, from their StandardWorkWeek and HolidayOrExceptions. * * Element names, units (hours) and the meaning of each value follow MPXJ's * PrimaveraPMFileReader, XmlProjectReader and XmlReaderHelper (see README.md): * percentages are fractions (0.25 = 25%), and a work time's Finish is a * minute before the end of the stretch (15:59 for 16:00). */ import { CalendarDay, CalendarException, Project, ProjectCalendar, WorkingTime } from './model'; import { XmlNode, child, children, parseXml, text } from './xml'; import { XerRow, XerTables, readP6 } from './xer'; /** A PMXML file: an XML document whose root element is APIBusinessObjects. */ export function isPmxml(src: string): boolean { // The root is the first element after the XML declaration and any comments. const m = /^\s*(?:<\?[\s\S]*?\?>\s*)?(?:\s*)*<(?:[\w.-]+:)?([\w.-]+)/.exec(src.slice(0, 4000)); return !!m && m[1] === 'APIBusinessObjects'; } const STATUS: Record = { 'Not Started': 'TK_NotStart', 'In Progress': 'TK_Active', Completed: 'TK_Complete' }; const TYPES: Record = { 'Task Dependent': 'TT_Task', 'Resource Dependent': 'TT_Rsrc', 'Level of Effort': 'TT_LOE', 'Start Milestone': 'TT_Mile', 'Finish Milestone': 'TT_FinMile', 'WBS Summary': 'TT_WBS', }; const PCT_TYPES: Record = { Physical: 'CP_Phys', Duration: 'CP_Drtn', Units: 'CP_Units' }; const RELATIONS: Record = { 'Finish to Start': 'PR_FS', 'Start to Start': 'PR_SS', 'Finish to Finish': 'PR_FF', 'Start to Finish': 'PR_SF' }; const CONSTRAINTS: Record = { 'Start On': 'CS_MSO', 'Start On or Before': 'CS_MSOB', 'Start On or After': 'CS_MSOA', 'Finish On': 'CS_MEO', 'Finish On or Before': 'CS_MEOB', 'Finish On or After': 'CS_MEOA', 'As Late As Possible': 'CS_ALAP', 'Mandatory Start': 'CS_MANDSTART', 'Mandatory Finish': 'CS_MANDFIN', }; const PRIORITY: Record = { Top: 'PT_Top', High: 'PT_High', Normal: 'PT_Normal', Low: 'PT_Low', Lowest: 'PT_Lowest' }; const RESOURCE_TYPES: Record = { Labor: 'RT_Labor', Nonlabor: 'RT_Equip', Material: 'RT_Mat' }; const CAL_TYPES: Record = { Global: 'CA_Base', Project: 'CA_Project', Resource: 'CA_Rsrc' }; const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; /** "2026-03-04T08:00:00" as XER writes it, "2026-03-04 08:00". */ function xerDate(v: string): string { const m = /^(\d{4}-\d\d-\d\d)(?:T(\d\d:\d\d))?/.exec(v); return m ? `${m[1]} ${m[2] ?? '00:00'}` : ''; } /** A fraction (0.25) as a percentage ("25"). */ function pct(v: string): string { return v === '' || !Number.isFinite(Number(v)) ? '' : String(Number(v) * 100); } const flag = (v: string) => (v === '1' || v === 'true' ? 'Y' : v ? 'N' : ''); /** Two amounts added, empty when both are. */ function sum(a: string, b: string): string { if (a === '' && b === '') return ''; return String((Number(a) || 0) + (Number(b) || 0)); } /** "08:00:00" in minutes; a Finish is the last minute worked, so the stretch ends a minute later. */ function clock(v: string, finish: boolean): number | null { const m = /^\s*(\d{1,2}):(\d\d)/.exec(v); if (!m) return null; const t = +m[1] * 60 + +m[2]; return finish ? t + 1 : t; } function workTimes(n: XmlNode): WorkingTime[] { const out: WorkingTime[] = []; for (const w of children(n, 'WorkTime')) { const start = clock(text(w, 'Start'), false); const end = clock(text(w, 'Finish'), true); if (start !== null && end !== null && end > start) out.push({ start, end }); } return out.sort((a, b) => a.start - b.start); } export function readPmxmlCalendar(c: XmlNode): ProjectCalendar { const week = new Map(); for (const h of children(child(c, 'StandardWorkWeek'), 'StandardWorkHours')) week.set(text(h, 'DayOfWeek'), h); // A day the calendar leaves out works 8 to 4 on weekdays, as MPXJ fills it in. const days: CalendarDay[] = DAYS.map((name, d) => { const h = week.get(name); if (!h) return d === 0 || d === 6 ? { type: 'nonworking', hours: [] } : { type: 'working', hours: [{ start: 480, end: 960 }] }; const hours = workTimes(h); return hours.length ? { type: 'working', hours } : { type: 'nonworking', hours: [] }; }); const exceptions: CalendarException[] = []; for (const e of children(child(c, 'HolidayOrExceptions'), 'HolidayOrException')) { const m = /^(\d{4})-(\d\d)-(\d\d)/.exec(text(e, 'Date')); if (!m) continue; const day = Date.UTC(+m[1], +m[2] - 1, +m[3]); // A day off is written with no work time, or as midnight to 23:59. const hours = workTimes(e).filter((h) => !(h.start === 0 && h.end === 1440)); exceptions.push({ name: '', from: day, to: day, hours, recurrence: '', dates: null }); } exceptions.sort((a, b) => a.from - b.from); const base = text(c, 'BaseCalendarObjectId'); return { uniqueId: Number(text(c, 'ObjectId')), name: text(c, 'Name') || 'Calendar', parent: base ? Number(base) : null, resource: null, days, exceptions, weeks: [] }; } /** The calendar's columns of XER's CALENDAR table that readP6 uses. */ function calendarRow(c: XmlNode, projId: string): XerRow { return { clndr_id: text(c, 'ObjectId'), clndr_name: text(c, 'Name'), clndr_type: CAL_TYPES[text(c, 'Type')] ?? '', proj_id: text(c, 'ProjectObjectId') || projId, base_clndr_id: text(c, 'BaseCalendarObjectId'), default_flag: flag(text(c, 'IsDefault')), day_hr_cnt: text(c, 'HoursPerDay'), week_hr_cnt: text(c, 'HoursPerWeek'), month_hr_cnt: text(c, 'HoursPerMonth'), }; } export function pmxmlTables(src: string): { tables: XerTables; calendars: ProjectCalendar[] } { const doc = parseXml(src); const root = child(doc, 'APIBusinessObjects'); if (!root) throw new Error('This XML file is not a Primavera P6 XML file.'); const rows = (name: string) => { const list: XerRow[] = []; return { name, list }; }; const PROJECT = rows('PROJECT'), CALENDAR = rows('CALENDAR'), PROJWBS = rows('PROJWBS'), TASK = rows('TASK'), TASKPRED = rows('TASKPRED'); const RSRC = rows('RSRC'), TASKRSRC = rows('TASKRSRC'), CURRTYPE = rows('CURRTYPE'), TASKMEMO = rows('TASKMEMO'), WBSMEMO = rows('WBSMEMO'); const calendars: ProjectCalendar[] = []; for (const c of children(root, 'Calendar')) { CALENDAR.list.push(calendarRow(c, '')); calendars.push(readPmxmlCalendar(c)); } for (const c of children(root, 'Currency')) CURRTYPE.list.push({ curr_symbol: text(c, 'Symbol') }); for (const r of children(root, 'Resource')) { RSRC.list.push({ rsrc_id: text(r, 'ObjectId'), rsrc_short_name: text(r, 'Id'), rsrc_name: text(r, 'Name'), email_addr: text(r, 'EmailAddress'), rsrc_type: RESOURCE_TYPES[text(r, 'ResourceType')] ?? '', def_qty_per_hr: text(r, 'DefaultUnitsPerTime'), clndr_id: text(r, 'CalendarObjectId'), rsrc_notes: text(r, 'ResourceNotes'), }); } // Projects, and the elements a project holds; the same elements at the top level (some tools write them there) belong to the project they name. const projects = children(root, 'Project'); const holders: [XmlNode, string][] = [...projects.map((p): [XmlNode, string] => [p, text(p, 'ObjectId')]), [root, '']]; projects.forEach((p, i) => { PROJECT.list.push({ proj_id: text(p, 'ObjectId'), proj_short_name: text(p, 'Id') || text(p, 'Name'), export_flag: i === 0 ? 'Y' : 'N', clndr_id: text(p, 'ActivityDefaultCalendarObjectId'), plan_start_date: xerDate(text(p, 'PlannedStartDate')), scd_end_date: xerDate(text(p, 'ScheduledFinishDate')), plan_end_date: xerDate(text(p, 'MustFinishByDate')), last_recalc_date: xerDate(text(p, 'DataDate')), name_sep_char: text(p, 'WBSCodeSeparator'), }); }); for (const [h, pid] of holders) { if (h !== root) { for (const c of children(h, 'Calendar')) { CALENDAR.list.push(calendarRow(c, pid)); calendars.push(readPmxmlCalendar(c)); } } for (const w of children(h, 'WBS')) { PROJWBS.list.push({ wbs_id: text(w, 'ObjectId'), proj_id: text(w, 'ProjectObjectId') || pid, parent_wbs_id: text(w, 'ParentObjectId'), wbs_short_name: text(w, 'Code'), wbs_name: text(w, 'Name'), seq_num: text(w, 'SequenceNumber'), }); } for (const a of children(h, 'Activity')) { TASK.list.push({ task_id: text(a, 'ObjectId'), proj_id: text(a, 'ProjectObjectId') || pid, wbs_id: text(a, 'WBSObjectId'), task_code: text(a, 'Id'), task_name: text(a, 'Name'), task_type: TYPES[text(a, 'Type')] ?? '', status_code: STATUS[text(a, 'Status')] ?? '', complete_pct_type: PCT_TYPES[text(a, 'PercentCompleteType')] ?? '', phys_complete_pct: pct(text(a, 'PhysicalPercentComplete')), act_work_qty: sum(text(a, 'ActualLaborUnits'), text(a, 'ActualNonLaborUnits')), remain_work_qty: sum(text(a, 'RemainingLaborUnits'), text(a, 'RemainingNonLaborUnits')), target_work_qty: sum(text(a, 'PlannedLaborUnits'), text(a, 'PlannedNonLaborUnits')), target_drtn_hr_cnt: text(a, 'PlannedDuration'), remain_drtn_hr_cnt: text(a, 'RemainingDuration'), act_start_date: xerDate(text(a, 'ActualStartDate')), act_end_date: xerDate(text(a, 'ActualFinishDate')), // P6's Start and Finish columns first, then the remaining early dates (MPXJ's order). restart_date: xerDate(text(a, 'StartDate') || text(a, 'RemainingEarlyStartDate')), reend_date: xerDate(text(a, 'FinishDate') || text(a, 'RemainingEarlyFinishDate')), target_start_date: xerDate(text(a, 'PlannedStartDate')), target_end_date: xerDate(text(a, 'PlannedFinishDate')), late_start_date: xerDate(text(a, 'RemainingLateStartDate')), late_end_date: xerDate(text(a, 'RemainingLateFinishDate')), total_float_hr_cnt: text(a, 'TotalFloat'), free_float_hr_cnt: text(a, 'FreeFloat'), driving_path_flag: flag(text(a, 'IsLongestPath')), cstr_type: CONSTRAINTS[text(a, 'PrimaryConstraintType')] ?? '', cstr_date: xerDate(text(a, 'PrimaryConstraintDate')), priority_type: PRIORITY[text(a, 'LevelingPriority')] ?? '', clndr_id: text(a, 'CalendarObjectId'), }); } for (const r of children(h, 'Relationship')) { TASKPRED.list.push({ task_id: text(r, 'SuccessorActivityObjectId'), pred_task_id: text(r, 'PredecessorActivityObjectId'), pred_type: RELATIONS[text(r, 'Type')] ?? 'PR_FS', lag_hr_cnt: text(r, 'Lag'), }); } for (const r of children(h, 'ResourceAssignment')) { TASKRSRC.list.push({ task_id: text(r, 'ActivityObjectId'), rsrc_id: text(r, 'ResourceObjectId'), proj_id: text(r, 'ProjectObjectId') || pid, act_reg_qty: text(r, 'ActualUnits'), remain_qty: text(r, 'RemainingUnits'), target_qty: text(r, 'PlannedUnits'), act_reg_cost: text(r, 'ActualCost'), remain_cost: text(r, 'RemainingCost'), target_cost: text(r, 'PlannedCost'), target_qty_per_hr: text(r, 'PlannedUnitsPerTime'), act_start_date: xerDate(text(r, 'ActualStartDate')), act_end_date: xerDate(text(r, 'ActualFinishDate')), restart_date: xerDate(text(r, 'RemainingStartDate')), reend_date: xerDate(text(r, 'RemainingFinishDate')), target_start_date: xerDate(text(r, 'PlannedStartDate')), target_end_date: xerDate(text(r, 'PlannedFinishDate')), }); } for (const n of children(h, 'ActivityNote')) TASKMEMO.list.push({ task_id: text(n, 'ActivityObjectId'), proj_id: text(n, 'ProjectObjectId') || pid, task_memo: text(n, 'Note') }); for (const n of children(h, 'ProjectNote')) if (text(n, 'WBSObjectId')) WBSMEMO.list.push({ wbs_id: text(n, 'WBSObjectId'), proj_id: text(n, 'ProjectObjectId') || pid, wbs_memo: text(n, 'Note') }); } const tables: XerTables = new Map(); for (const t of [PROJECT, CALENDAR, PROJWBS, TASK, TASKPRED, RSRC, TASKRSRC, CURRTYPE, TASKMEMO, WBSMEMO]) tables.set(t.name, t.list); return { tables, calendars }; } export function readPmxml(src: string): Project { const { tables, calendars } = pmxmlTables(src); return readP6(tables, { format: 'Primavera P6 (XML)', file: 'P6 XML file', calendars }); }