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

NaturalIntelligence / fast-xml-parser / 13817442089

12 Mar 2025 05:13PM UTC coverage: 98.946%. Remained the same
13817442089

Pull #734

github

web-flow
Merge 4a6d4de32 into af4f1d2d4
Pull Request #734: [WIP] ci: build with Node 18.x

1087 of 1120 branches covered (97.05%)

8640 of 8732 relevant lines covered (98.95%)

510597.48 hits per line

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

94.64
/src/xmlparser/XMLParser.js
1
import { buildOptions} from './OptionsBuilder.js';
5✔
2
import OrderedObjParser from './OrderedObjParser.js';
5✔
3
import prettify from './node2json.js';
5✔
4
import {validate} from "../validator.js";
5✔
5

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

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