Skip to content

Instantly share code, notes, and snippets.

Avatar
😃
Writing Code and Having Fun :)

Abuzer Asif AbuzerAsif

😃
Writing Code and Having Fun :)
View GitHub Profile
View nodejs-xml-to-json-1.js
const xml2js = require('xml2js');
const xml = '<root>Hello world!</root>'
xml2js.parseString(xml, function (err, result) {
console.log(result.root); // Output: Hello world!
});
View nodejs-xml-to-json-2.js
const xml2js = require('xml2js');
const xml = '<root>Hello world!</root>'
xml2js.parseString(xml, function (err, result) {
console.log(result.root); // Output: Hello world!
const json = JSON.stringify(result);
console.log(json); // Output: {"root":"Hello world!"}
View nodejs-xml-to-json-3.js
const xml2js = require('xml2js');
const productXML = '<Product><ID>10</ID><Name>Pizza</Name></Product>';
xml2js.parseString(productXML, function (err, result) {
// console.dir will allow us to print the whole object in our console
console.dir(result); // Output: { Product: { ID: [ '10' ], Name: [ 'Pizza' ] } }
console.dir(result.Product); // Output: { ID: [ '10' ], Name: [ 'Pizza' ] }
console.log(result.Product.ID[0]); // Output: 10
console.log(result.Product.Name[0]); // Output: Pizza
View nodejs-xml-to-json-4.js
const xml2js = require('xml2js');
const productXML = '<Product><ID>10</ID><Name>Pizza</Name></Product>';
xml2js.parseString(productXML, { explicitArray : false }, function (err, result) {
// console.dir will allow us to print the whole object in our console
console.dir(result); // Output: { Product: { ID: '10', Name: 'Pizza' } }
console.dir(result.Product); // Output: { ID: '10', Name: 'Pizza' }
console.log(result.Product.ID); // Output: 10
console.log(result.Product.Name); // Output: Pizza
View nodejs-xml-to-json-5.js
const xml2js = require('xml2js');
const productXML = '<Product><ID>10</ID><Name>Pizza</Name></Product>';
xml2js.parseString(productXML, { explicitArray : false }, function (err, result) {
// console.dir will allow us to print the whole object in our console
console.dir(result); // Output: { Product: { ID: '10', Name: 'Pizza' } }
console.dir(result.Product); // Output: { ID: '10', Name: 'Pizza' }
console.log(result.Product.ID); // Output: 10
console.log(result.Product.Name); // Output: Pizza
View angular-custom-pipes-5.html
<h1>{{ 500 | currency : 'GBP' }}</h1> // Output: £500
View angular-custom-pipes-6.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'myCustomPipe'
})
export class MyCustomPipe implements PipeTransform {
transform(value: any, args?: any): any {
return null;
}
}
View angular-custom-pipes-7.html
<h1>{{ 'Hello world' | myCustomPipe }}</h1> // Output is empty
View angular-custom-pipes-8.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'myCustomPipe'
})
export class MyCustomPipe implements PipeTransform {
transform(value: any, args?: any): any {
return value;
}
}
View angular-custom-pipes-9.html
<h1>{{ 100 | myCustomPipe: 5: 10 }}</h1> // Output: 50