* Minimal XML parser tuned for Workday SOAP responses: namespaced tags, * attributes, mixed text, self-closing tags, and CDATA sections. * Not a general-purpose parser — it does not expand entities beyond the * standard five and ignores processing instructions and DOCTYPE.
(xml: string)
| 331 | * standard five and ignores processing instructions and DOCTYPE. |
| 332 | */ |
| 333 | function parseXml(xml: string): XmlNode { |
| 334 | let i = 0 |
| 335 | const len = xml.length |
| 336 | |
| 337 | function skipWhitespace() { |
| 338 | while (i < len && xml.charCodeAt(i) <= 32) i++ |
| 339 | } |
| 340 | |
| 341 | function readName(): string { |
| 342 | const start = i |
| 343 | while (i < len) { |
| 344 | const c = xml[i] |
| 345 | if ( |
| 346 | c === ' ' || |
| 347 | c === '\t' || |
| 348 | c === '\n' || |
| 349 | c === '\r' || |
| 350 | c === '>' || |
| 351 | c === '/' || |
| 352 | c === '=' |
| 353 | ) |
| 354 | break |
| 355 | i++ |
| 356 | } |
| 357 | return xml.slice(start, i) |
| 358 | } |
| 359 | |
| 360 | function readAttributes(): Record<string, string> { |
| 361 | const attrs: Record<string, string> = {} |
| 362 | while (i < len) { |
| 363 | skipWhitespace() |
| 364 | const c = xml[i] |
| 365 | if (c === '>' || c === '/' || c === '?') return attrs |
| 366 | const name = readName() |
| 367 | skipWhitespace() |
| 368 | if (xml[i] !== '=') { |
| 369 | attrs[name] = '' |
| 370 | continue |
| 371 | } |
| 372 | i++ // = |
| 373 | skipWhitespace() |
| 374 | const quote = xml[i] |
| 375 | if (quote !== '"' && quote !== "'") { |
| 376 | attrs[name] = '' |
| 377 | continue |
| 378 | } |
| 379 | i++ |
| 380 | const start = i |
| 381 | while (i < len && xml[i] !== quote) i++ |
| 382 | attrs[name] = decodeEntities(xml.slice(start, i)) |
| 383 | if (i < len) i++ // closing quote |
| 384 | } |
| 385 | return attrs |
| 386 | } |
| 387 | |
| 388 | function decodeEntities(s: string): string { |
| 389 | return s.replace(/&(amp|lt|gt|quot|apos|#\d+|#x[0-9a-fA-F]+);/g, (_, ent) => { |
| 390 | switch (ent) { |
no test coverage detected