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

Method parse

src/xmlparser/XMLParser.js:20–45  ·  view source on GitHub ↗

* Parse XML dats to JS object * @param {string|Uint8Array} xmlData * @param {boolean|Object} validationOption

(xmlData, validationOption)

Source from the content-addressed store, hash-verified

18 * @param {boolean|Object} validationOption
19 */
20 parse(xmlData, validationOption) {
21 if (typeof xmlData !== "string" && xmlData.toString) {
22 if (xmlData instanceof Uint8Array &&
23 !(typeof Buffer !== "undefined" && Buffer.isBuffer(xmlData))) {
24 xmlData = new TextDecoder("utf-8", { ignoreBOM: true }).decode(xmlData);
25 } else {
26 xmlData = xmlData.toString();
27 }
28 } else if (typeof xmlData !== "string") {
29 throw new Error("XML data is accepted in String or Bytes[] form.")
30 }
31
32 if (validationOption) {
33 if (validationOption === true) validationOption = {}; //validate with default options
34
35 const result = validate(xmlData, validationOption);
36 if (result !== true) {
37 throw Error(`${result.err.msg}:${result.err.line}:${result.err.col}`)
38 }
39 }
40 const orderedObjParser = new OrderedObjParser(this.options, this.externalEntities);
41 // orderedObjParser.entityDecoder.setExternalEntities(this.externalEntities);
42 const orderedResult = orderedObjParser.parseXml(xmlData);
43 if (this.options.preserveOrder || orderedResult === undefined) return orderedResult;
44 else return prettify(orderedResult, this.options, orderedObjParser.matcher, orderedObjParser.readonlyMatcher);
45 }
46
47 /**
48 * Add Entity which is not by default supported by this library

Callers

nothing calls this directly

Calls 5

validateFunction · 0.90
prettifyFunction · 0.85
parseXmlMethod · 0.80
decodeMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected