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

NaturalIntelligence / fast-xml-parser / 13532857399

25 Feb 2025 11:15PM UTC coverage: 98.946% (+0.7%) from 98.217%
13532857399

push

github

web-flow
Update node.js.yml

1087 of 1120 branches covered (97.05%)

8639 of 8731 relevant lines covered (98.95%)

408524.79 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';
4✔
2
import OrderedObjParser from './OrderedObjParser.js';
4✔
3
import prettify from './node2json.js';
4✔
4
import {validate} from "../validator.js";
4✔
5

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

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