MCPcopy Create free account
hub / github.com/NaturalIntelligence/fast-xml-parser / parseXml

Function parseXml

src/xmlparser/OrderedObjParser.js:292–614  ·  view source on GitHub ↗
(xmlData)

Source from the content-addressed store, hash-verified

290 }
291}
292const parseXml = function (xmlData) {
293 const options = this.options;
294 const crlfPositions = options.captureMetaData ? [] : undefined;
295 xmlData = xmlData.replace(/\r\n?/g, options.captureMetaData ? (newline, index) => {
296 if (newline.length === 2) {
297 // Store the position after earlier CRLF pairs have been normalized.
298 crlfPositions.push(index - crlfPositions.length);
299 }
300 return "\n";
301 } : "\n");
302 const xmlObj = new xmlNode('!xml');
303 let currentNode = xmlObj;
304 let textData = "";
305
306 // Reset matcher for new document
307 this.matcher.reset();
308 this.entityDecoder.reset();
309
310 // Reset entity expansion counters for this document
311 this.entityExpansionCount = 0;
312 this.currentExpandedLength = 0;
313 this.doctypefound = false;
314 const docTypeReader = new DocTypeReader(options.processEntities);
315 const xmlLen = xmlData.length;
316 for (let i = 0; i < xmlLen; i++) {//for each char in XML data
317 const ch = xmlData[i];
318 if (ch === '<') {
319 // const nextIndex = i+1;
320 // const _2ndChar = xmlData[nextIndex];
321 const c1 = xmlData.charCodeAt(i + 1);
322 if (c1 === 47) {//Closing Tag '/'
323 const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
324 let tagName = xmlData.substring(i + 2, closeIndex).trim();
325
326 if (options.removeNSPrefix) {
327 const colonIndex = tagName.indexOf(":");
328 if (colonIndex !== -1) {
329 tagName = tagName.substr(colonIndex + 1);
330 }
331 }
332
333 tagName = transformTagName(options.transformTagName, tagName, "", options).tagName;
334
335 if (currentNode) {
336 textData = this.saveTextToParentTag(textData, currentNode, this.readonlyMatcher);
337 }
338
339 //check if last tag of nested tag was unpaired tag
340 const lastTagName = this.matcher.getCurrentTag();
341 if (tagName && options.unpairedTagsSet.has(tagName)) {
342 throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
343 }
344 if (lastTagName && options.unpairedTagsSet.has(lastTagName)) {
345 // Pop the unpaired tag
346 this.matcher.pop();
347 this.tagsNodeStack.pop();
348 }
349 // Pop the closing tag

Callers

nothing calls this directly

Calls 15

setXmlVersionMethod · 0.95
readDocTypeMethod · 0.95
findClosingIndexFunction · 0.85
transformTagNameFunction · 0.85
getOriginalIndexFunction · 0.85
extractNamespaceFunction · 0.85
extractRawAttributesFunction · 0.85
readTagExpFunction · 0.70
pushMethod · 0.45
resetMethod · 0.45
getCurrentTagMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected