11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import { j2xParser , parse , validate } from "fast-xml-parser" ;
4+ import { XMLBuilder , XMLValidator , XMLParser } from "fast-xml-parser" ;
55import { XML_ATTRKEY , XML_CHARKEY , XmlOptions } from "./xml.common" ;
66
77function getCommonOptions ( options : XmlOptions ) {
88 return {
9- attrNodeName : XML_ATTRKEY ,
9+ attributesGroupName : XML_ATTRKEY ,
1010 textNodeName : options . xmlCharKey ?? XML_CHARKEY ,
1111 ignoreAttributes : false ,
12+ suppressBooleanAttributes : false ,
1213 } ;
1314}
1415
@@ -17,7 +18,7 @@ function getSerializerOptions(options: XmlOptions = {}) {
1718 ...getCommonOptions ( options ) ,
1819 attributeNamePrefix : "@_" ,
1920 format : true ,
20- supressEmptyNode : true ,
21+ suppressEmptyNode : true ,
2122 indentBy : "" ,
2223 rootNodeName : options . rootName ?? "root" ,
2324 } ;
@@ -27,7 +28,7 @@ function getParserOptions(options: XmlOptions = {}) {
2728 return {
2829 ...getCommonOptions ( options ) ,
2930 parseAttributeValue : false ,
30- parseNodeValue : false ,
31+ parseTagValue : false ,
3132 attributeNamePrefix : "" ,
3233 } ;
3334}
@@ -39,11 +40,11 @@ function getParserOptions(options: XmlOptions = {}) {
3940 */
4041export function stringifyXML ( obj : unknown , opts : XmlOptions = { } ) : string {
4142 const parserOptions = getSerializerOptions ( opts ) ;
42- const j2x = new j2xParser ( parserOptions ) ;
43+ const j2x = new XMLBuilder ( parserOptions ) ;
4344
4445 const node = { [ parserOptions . rootNodeName ] : obj } ;
4546
46- const xmlData : string = j2x . parse ( node ) ;
47+ const xmlData : string = j2x . build ( node ) ;
4748 return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${ xmlData } ` . replace ( / \n / g, "" ) ;
4849}
4950
@@ -58,13 +59,14 @@ export async function parseXML(str: string, opts: XmlOptions = {}): Promise<any>
5859 throw new Error ( "Document is empty" ) ;
5960 }
6061
61- const validation = validate ( str ) ;
62+ const validation = XMLValidator . validate ( str ) ;
6263
6364 if ( validation !== true ) {
6465 throw validation ;
6566 }
6667
67- const parsedXml = parse ( unescapeHTML ( str ) , getParserOptions ( opts ) ) ;
68+ const parser = new XMLParser ( getParserOptions ( opts ) ) ;
69+ const parsedXml = parser . parse ( unescapeHTML ( str ) ) ;
6870
6971 if ( ! opts . includeRoot ) {
7072 for ( const key of Object . keys ( parsedXml ) ) {
0 commit comments