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

NaturalIntelligence / fast-xml-parser / 14973142022

12 May 2025 01:11PM CUT coverage: 97.547%. Remained the same
14973142022

push

github

web-flow
export types in fxp.d.ts for better module usability (#744)

1090 of 1134 branches covered (96.12%)

8908 of 9132 relevant lines covered (97.55%)

488239.21 hits per line

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

95.77
/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
import XmlNode from './xmlNode.js';
5✔
6

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

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

5✔
58
    /**
5✔
59
     * Returns a Symbol that can be used to access the metadata
5✔
60
     * property on a node.
5✔
61
     * 
5✔
62
     * If Symbol is not available in the environment, an ordinary property is used
5✔
63
     * and the name of the property is here returned.
5✔
64
     * 
5✔
65
     * The XMLMetaData property is only present when `captureMetaData`
5✔
66
     * is true in the options.
5✔
67
     */
5✔
68
    static getMetaDataSymbol() {
5✔
69
        return XmlNode.getMetaDataSymbol();
5✔
70
    }
5✔
71
}
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