• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalIntelligence / fast-xml-parser / 4156981534

pending completion
4156981534

push

github

amit kumar gupta
Update package detail

692 of 741 branches covered (93.39%)

3 of 3 new or added lines in 2 files covered. (100.0%)

2376 of 2418 relevant lines covered (98.26%)

196233.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/xmlparser/XMLParser.js
1
const { buildOptions} = require("./OptionsBuilder");
1✔
2
const OrderedObjParser = require("./OrderedObjParser");
1✔
3
const { prettify} = require("./node2json");
1✔
4
const validator = require('../validator');
1✔
5

6
class XMLParser{
7
    
8
    constructor(options){
9
        this.externalEntities = {};
146✔
10
        this.options = buildOptions(options);
146✔
11
        
12
    }
13
    /**
14
     * Parse XML dats to JS object 
15
     * @param {string|Buffer} xmlData 
16
     * @param {boolean|Object} validationOption 
17
     */
18
    parse(xmlData,validationOption){
19
        if(typeof xmlData === "string"){
143✔
20
        }else if( xmlData.toString){
1!
21
            xmlData = xmlData.toString();
1✔
22
        }else{
23
            throw new Error("XML data is accepted in String or Bytes[] form.")
×
24
        }
25
        if( validationOption){
143✔
26
            if(validationOption === true) validationOption = {}; //validate with default options
9!
27
            
28
            const result = validator.validate(xmlData, validationOption);
9✔
29
            if (result !== true) {
9✔
30
              throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` )
1✔
31
            }
32
          }
33
        const orderedObjParser = new OrderedObjParser(this.options);
142✔
34
        orderedObjParser.addExternalEntities(this.externalEntities);
142✔
35
        const orderedResult = orderedObjParser.parseXml(xmlData);
142✔
36
        if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
136✔
37
        else return prettify(orderedResult, this.options);
110✔
38
    }
39

40
    /**
41
     * Add Entity which is not by default supported by this library
42
     * @param {string} key 
43
     * @param {string} value 
44
     */
45
    addEntity(key, value){
46
        if(value.indexOf("&") !== -1){
7✔
47
            throw new Error("Entity value can't have '&'")
1✔
48
        }else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
6✔
49
            throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'")
2✔
50
        }else if(value === "&"){
4!
51
            throw new Error("An entity with value '&' is not permitted");
×
52
        }else{
53
            this.externalEntities[key] = value;
4✔
54
        }
55
    }
56
}
57

58
module.exports = XMLParser;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc