UNPKG

2.69 kBPlain TextView Raw
1import { Element, ElementCompact } from './index'
2import * as convert from './index'
3
4// Declaration
5const declarationCompact1: ElementCompact = { _declaration: { _attributes: { version: 2 } }};
6const declarationCompact2: ElementCompact = { _declaration: { _attributes: { version: '1.0', encoding: 'utf-8' }}};
7const declaration1: Element = { declaration: { }};
8const declaration2: Element = { declaration: { attributes: { version: '1.0', encoding: 'utf-8' }}};
9
10// Processing Instruction
11const instructionCompact: ElementCompact = { _instruction: { go: 'there' }};
12const instruction: Element = { elements:[{ type: 'instruction', name: 'go', instruction: 'there' }]};
13
14// Comment
15const commentCompact: ElementCompact = { _comment : 'Hello, World!' };
16const comment: Element = { elements: [{ type: 'comment', comment: 'Hello, World!' }]};
17
18// CDATA
19const cdataCompact: ElementCompact = { _cdata: '<foo></bar>' };
20const cdata: Element = { elements : [{ type: 'cdata', cdata: '<foo></bar>' }]};
21
22// Element
23const elementCompact1: ElementCompact = { a: {} };
24const element1: Element = { elements:[{ type: 'element', name: 'a' }]};
25
26const elementCompact2: ElementCompact = { a: { _attributes: { x: '1.234', y:'It\'s' }}};
27const element2: Element = { elements: [{ type: 'element', name: 'a', attributes: { x: '1.234', y: 'It\'s' }}]};
28
29const elementCompact3: ElementCompact = { a: { _text: ' Hi ' }};
30const element3: Element = { elements:[{ type: 'element', name: 'a', elements: [{ type: 'text', text: ' Hi ' }]}]};
31
32const elementCompact4: ElementCompact = { a: {}, b: {} };
33const element4: Element = { elements:[{ type: 'element', name: 'a' }, { type: 'element', name: 'b' }]};
34
35const elementCompact5: ElementCompact = { a: { b: {} }};
36const element5: Element = { elements: [{ type: 'element', name: 'a', elements: [{ type: 'element', name: 'b' }]}]};
37
38const xml = `
39<?xml version="1.0" encoding="utf-8"?>
40<note importance="high" logged="true">
41 <title>Happy</title>
42 <todo>Work</todo>
43 <todo>Play</todo>
44</note>`;
45
46// xml2js
47let jsResult1: any = convert.xml2js(xml, {compact:true});
48let jsResult2: any = convert.xml2js(xml, {compact:false});
49
50// xml2json
51let jsonResult1: string = convert.xml2json(xml, {compact:true, spaces:4});
52let jsonResult2: string = convert.xml2json(xml, {compact:false});
53
54// js2xml
55let xmlResult1: string = convert.js2xml({a:{}}, { compact:true, spaces:4});
56let xmlResult2: string = convert.js2xml({elements:[{type:'element', name:'a'}]}, {compact:false});
57
58// json2xml
59let xmlResult3: string = convert.json2xml('{"a":{}}', { compact:true, spaces:4});
60let xmlResult4: string = convert.json2xml('{"elements":[{"type":"element","name":"a"}]}', {compact:false});