|
| 1 | +/** |
| 2 | + * Copyright (C) 2016-2019 Michael Kourlas |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* tslint:disable:no-console object-literal-sort-keys */ |
| 18 | + |
| 19 | +import {parse} from "../lib/main"; |
| 20 | + |
| 21 | +/** |
| 22 | + * This example demonstrates a very simple usage of js2xmlparser. |
| 23 | + */ |
| 24 | +const example1 = () => { |
| 25 | + const obj = { |
| 26 | + "firstName": "John", |
| 27 | + "lastName": "Smith" |
| 28 | + }; |
| 29 | + console.log(parse("person", obj)); |
| 30 | + console.log(); |
| 31 | +}; |
| 32 | +example1(); |
| 33 | + |
| 34 | +/** |
| 35 | + * This example demonstrates a more complex usage of j2xmlparser. |
| 36 | + */ |
| 37 | +const example2 = () => { |
| 38 | + const obj = { |
| 39 | + "firstName": "John", |
| 40 | + "lastName": "Smith", |
| 41 | + "dateOfBirth": new Date(1964, 7, 26), |
| 42 | + "address": { |
| 43 | + "@": { |
| 44 | + "type": "home" |
| 45 | + }, |
| 46 | + "streetAddress": "3212 22nd St", |
| 47 | + "city": "Chicago", |
| 48 | + "state": "Illinois", |
| 49 | + "zip": 10000 |
| 50 | + }, |
| 51 | + "phone": [ |
| 52 | + { |
| 53 | + "@": { |
| 54 | + "type": "home" |
| 55 | + }, |
| 56 | + "#": "123-555-4567" |
| 57 | + }, |
| 58 | + { |
| 59 | + "@": { |
| 60 | + "type": "cell" |
| 61 | + }, |
| 62 | + "#": "890-555-1234" |
| 63 | + }, |
| 64 | + { |
| 65 | + "@": { |
| 66 | + "type": "work" |
| 67 | + }, |
| 68 | + "#": "567-555-8901" |
| 69 | + } |
| 70 | + ], |
| 71 | + "email": "john@smith.com" |
| 72 | + }; |
| 73 | + console.log(parse("person", obj)); |
| 74 | + console.log(); |
| 75 | +}; |
| 76 | +example2(); |
| 77 | + |
| 78 | +/** |
| 79 | + * This example demonstrates some of js2xmlparser's options. |
| 80 | + */ |
| 81 | +const example3 = () => { |
| 82 | + const options = { |
| 83 | + aliasString: "exAlias", |
| 84 | + attributeString: "exAttr", |
| 85 | + cdataKeys: [ |
| 86 | + "exCdata", |
| 87 | + "exCdata2" |
| 88 | + ], |
| 89 | + declaration: { |
| 90 | + include: true, |
| 91 | + encoding: "UTF-16", |
| 92 | + standalone: "yes", |
| 93 | + version: "1.1" |
| 94 | + }, |
| 95 | + dtd: { |
| 96 | + include: true, |
| 97 | + name: "exName", |
| 98 | + sysId: "exSysId", |
| 99 | + pubId: "exPubId" |
| 100 | + }, |
| 101 | + format: { |
| 102 | + doubleQuotes: true, |
| 103 | + indent: "\t", |
| 104 | + newline: "\r\n", |
| 105 | + pretty: true |
| 106 | + }, |
| 107 | + typeHandlers: { |
| 108 | + "[object Number]": (value: any) => { |
| 109 | + return value + 17; |
| 110 | + } |
| 111 | + }, |
| 112 | + valueString: "exVal", |
| 113 | + wrapHandlers: { |
| 114 | + "exArr": () => { |
| 115 | + return "exArrInner"; |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + const obj = { |
| 121 | + "ex1": "ex2", |
| 122 | + "exVal_1": 123, |
| 123 | + "ex3": ["ex4", "ex5"], |
| 124 | + "ex6": { |
| 125 | + "exAttr_1": { |
| 126 | + "ex7": "ex8", |
| 127 | + "ex9": "ex10" |
| 128 | + }, |
| 129 | + "ex11": "ex12", |
| 130 | + "ex13": { |
| 131 | + "ex14": "ex15" |
| 132 | + }, |
| 133 | + "ex16": [ |
| 134 | + "ex17", |
| 135 | + { |
| 136 | + "ex18": "ex19" |
| 137 | + } |
| 138 | + ], |
| 139 | + "exArr": [ |
| 140 | + "ex20", |
| 141 | + { |
| 142 | + "ex21": "ex22" |
| 143 | + } |
| 144 | + ], |
| 145 | + "exAttr_2": { |
| 146 | + "ex23": "ex24", |
| 147 | + "ex25": "ex26" |
| 148 | + } |
| 149 | + }, |
| 150 | + "exVal_2": "ex27", |
| 151 | + "ex28": [ |
| 152 | + "ex29", |
| 153 | + "ex30" |
| 154 | + ], |
| 155 | + "ex31": true, |
| 156 | + "ex32": undefined, |
| 157 | + "ex33": null, |
| 158 | + "ex34": 3.4, |
| 159 | + "ex35": () => { |
| 160 | + return "ex36"; |
| 161 | + }, |
| 162 | + "ex37": "i<j&", |
| 163 | + "exCdata": "i<j&", |
| 164 | + "exCdata2": "ddd", |
| 165 | + "ex38": { |
| 166 | + "exAlias": "ex39", |
| 167 | + "ex40": "ex41" |
| 168 | + }, |
| 169 | + "ex42": ["ex43", "ex44", "ex45", ["ex46", "ex47"], |
| 170 | + new Set(["ex48", "ex49"])], |
| 171 | + "ex50": new Map([ |
| 172 | + ["ex51", "ex52"], |
| 173 | + ["exVal_1", "ex53"], |
| 174 | + ["ex54", "ex55"], |
| 175 | + ["exAttr_1", |
| 176 | + { |
| 177 | + "ex56": "ex57", |
| 178 | + "ex58": "ex59" |
| 179 | + } |
| 180 | + ] as any |
| 181 | + ]) |
| 182 | + }; |
| 183 | + console.log(parse("ex0", obj, options)); |
| 184 | + console.log(); |
| 185 | +}; |
| 186 | +example3(); |
0 commit comments