Fast XML Parser was initially developed to support npm package Stubmatic which is used for the rapid development of mock web services. Now Fast XML Parser has more than 9 million weekly downloads and approximately half a million projects on GitHub are using it including Microsoft, IBM, AWS, and many other big companies.
In this post, we’ll talk about new features launched in V4.1.4 and v4.2.0
đź“Ś Info
Use oneListGroup
property to guide builder not to generate separate parent tag for each child of a list. Use updateTag
method with parer to update attrbutes name, attributes values, and tag name. You can also skip a tag from parsing result.
oneListGroup
for XML builder
Ideally there is no group needed in XML for repetitive tags. But having it may make more sense to understand the data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Addresses: [
{
Address: {
line1: ""
line2: ""
line3: ""
city>: ""
}
},{
Address: {
line1: ""
line2: ""
line3: ""
city>: ""
}
}
]
XML Builder was previously enclosing each Address
in Addresses
tag separately before v4.1.4. Now, if you set oneListGroup: true
as an option to Builder it makes only one group same as in the above JSON.
1
2
3
4
5
const builder = new XMLBuilder({
oneListGroup:"true",
format: true
});
const result = builder.build(jObj);
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Addresses>
<Address>
<line1></line1>
<line2></line2>
<line3></line3>
<city></city>
</Address>
<Address>
<line1></line1>
<line2></line2>
<line3></line3>
<city></city>
</Address>
</Addresses>
Change Attributes
Sometimes incoming XML doesn’t fit to our expectations. We may want to skip a few attributes of a tag from the parsing result. Sometimes we want to aggregate all the attributes in one or just change the value or name of some attributes. updateTag
method in parser’s option gives the independency to manage attributes in your way.
Input
1
2
3
4
5
6
7
<root>
<a keep="me" skip="me"></a>
<a skip="me"></a>
<a need="friend"></a>
<a camel="case" MakeMe="lower"></a>
<b change="val"></b>
</root>
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
updateTag(tagName, jPath, attrs){
if(attrs["skip"]) delete attrs["skip"]
if(attrs["camel"]) {
attrs["Camel"] = attrs["camel"];
delete attrs["camel"];
}
if(attrs["need"]) {
attrs["friend"] = "me";
}
if(attrs["MakeMe"]) {
attrs["makeme"] = attrs["MakeMe"];
delete attrs["MakeMe"];
}
if(attrs["change"]) {
attrs["change"] = attrs["change"].toUpperCase();
}
return tagName;
}
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"root": {
"a": [
{
"keep": "me"
},
"",
{
"need": "friend",
"friend": "me"
},
{
"Camel": "case",
"makeme": "lower"
}
],
"b": {
"change": "VAL"
}
}
}
Change Tag name or skip a tag
Though updateTag
doesn’t give you full control to manage tag but you can change tag name or skip a tag from the parsing result. Suppose, you’re parsing a HTML document and want to skip all the script tags or you want to change the name of a tag when it is not properly formatted. You may also conditionally skip a tag like to skip img
tag for big images.
Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<header></header>
<body>
<h1 class="highlight" >Post title</h1>
<content>
<img width="200" height="500">
<p>some text</p>
<img width="200" height="200">
<p join="a" all="b" in="c" one="d" >some text 2</p>
<img width="500" height="500">
</content>
<script></script>
</body>
</html>
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const options = {
ignoreAttributes: false,
attributeNamePrefix: "",
updateTag: function(tagname, jPath, attrs){
if(tagname ==="h1" && attrs["class"] && attrs["class"].indexOf("highlight") > -1){
attrs["class"] += " underline"
}else if(attrs["join"]){
let val = "";
Object.keys(attrs).forEach( a => {
val+= attrs[a]
delete attrs[a];
});
attrs["joint"] = val;
}
if(tagname === "script") return false;
else if(tagname === "img"){
if(attrs.width > 200 || attrs.height > 200) return false;
}else if(tagname === "content"){
return "div"
}
return tagname;
},
unpairedTags: ["img"]
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"html": {
"header": "",
"body": {
"h1": {
"#text": "Post title",
"class": "highlight underline"
},
"div": {
"p": [
"some text",
{
"#text": "some text 2",
"joint": "abcd"
}
],
"img": {
"width": "200",
"height": "200"
}
}
}
}
}
Your feedback is really important. So I can improve my future articles and correct what I have already published. You can also suggest if you want me to write an article on particular topic. If you really like this blog, or article, please share with others. That's the only way I can buy time to write more articles.