租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

734 lines
14 KiB

6 months ago
  1. # jstoxml
  2. [![npm downloads](https://img.shields.io/npm/dm/jstoxml.svg)](https://www.npmjs.com/package/jstoxml)
  3. ### Convert JavaScript objects (and JSON) to XML (for RSS, Podcasts, etc.)
  4. Everyone loves JSON, and more and more folks want to move that direction, but we still need things outputted in XML! Particularly for [RSS feeds](http://www.rssboard.org/rss-specification) and [Podcasts](http://www.apple.com/itunes/podcasts/specs.html).
  5. This is inspired by [node-jsontoxml](https://github.com/soldair/node-jsontoxml), which was found to be a bit too rough around the edges. jstoxml attempts to fix that by being more flexible.
  6. ### Installation
  7. - npm install jstoxml
  8. ### Changelog
  9. #### Version 2.2.0
  10. * Initial support for XML comments ([#47](https://github.com/davidcalhoun/jstoxml/issues/47))
  11. #### Version 2.1.1
  12. * Fix for [#48](https://github.com/davidcalhoun/jstoxml/issues/48) (various 0-depth issues, bad "is output start" logic)
  13. #### Version 2.0.0 (breaking)
  14. - New: automatic entity escaping for `&`, `<`, and `>` characters. In addition, quotes `"` in attributes are also escaped (see [#41](/../../issues/41)). Prior to this, users [had to provide their own filter manually](https://github.com/davidcalhoun/jstoxml/issues/4#issuecomment-19165730). Note that `jstoxml` makes an effort not to escape entities that appear to have already been encoded, to prevent double-encoding issues.
  15. - E.g. `toXML({ foo: '1 < 2 & 2 > 1' }); // -> "<foo>1 &lt; 2 &amp; 2 &gt; 1</foo>"`
  16. - To restore the default behavior from `v1.x.x`, simply pass in `false` to `filter` and `attributesFilter` options:
  17. `toXML({ foo: '1 < 2 & 2 > 1' }, { filter: false, attributesFilter: false }); // -> "<foo>1 < 2 & 2 > 1</foo>"`
  18. #### Past changes
  19. - See CHANGELOG.md for a full history of changes.
  20. ### Examples
  21. First you'll want to require jstoxml in your script, and assign the result to the namespace variable you want to use (in this case jstoxml):
  22. ```javascript
  23. // Node
  24. const { toXML } = require("jstoxml");
  25. // Browser (with the help of something like Webpack or Rollup)
  26. import { toXML } from "jstoxml";
  27. // Browser global fallback (requires no bundler)
  28. var toXML = window.jstoxml.toXML;
  29. ```
  30. #### Example 1: Simple object
  31. ```javascript
  32. toXML({
  33. foo: "bar",
  34. foo2: "bar2",
  35. });
  36. ```
  37. Output:
  38. ```
  39. <foo>bar</foo><foo2>bar2</foo2>
  40. ```
  41. Note: because JavaScript doesn't allow duplicate key names, only the last defined key will be outputted. If you need duplicate keys, please use an array instead (see Example 2 below).
  42. #### Example 2: Simple array (needed for duplicate keys)
  43. ```javascript
  44. toXML([
  45. {
  46. foo: "bar",
  47. },
  48. {
  49. foo: "bar2",
  50. },
  51. ]);
  52. ```
  53. Output:
  54. ```
  55. <foo>bar</foo><foo>bar2</foo>
  56. ```
  57. #### Example 3: Simple functions
  58. ```javascript
  59. toXML({ currentTime: () => new Date() });
  60. ```
  61. Output:
  62. ```
  63. <currentTime>Mon Oct 02 2017 09:34:54 GMT-0700 (PDT)</currentTime>
  64. ```
  65. #### Example 4: XML tag attributes
  66. ```javascript
  67. toXML({
  68. _name: "foo",
  69. _content: "bar",
  70. _attrs: {
  71. a: "b",
  72. c: "d",
  73. },
  74. });
  75. ```
  76. Output:
  77. ```
  78. <foo a="b" c="d">bar</foo>
  79. ```
  80. #### Example 5: Tags mixed with text content
  81. To output text content, set a key to null:
  82. ```javascript
  83. toXML({
  84. text1: null,
  85. foo: "bar",
  86. text2: null,
  87. });
  88. ```
  89. Output:
  90. ```
  91. text1<foo>bar</foo>text2
  92. ```
  93. #### Example 6: Nested tags (with indenting)
  94. ```javascript
  95. const xmlOptions = {
  96. header: false,
  97. indent: " ",
  98. };
  99. toXML(
  100. {
  101. a: {
  102. foo: "bar",
  103. foo2: "bar2",
  104. },
  105. },
  106. xmlOptions
  107. );
  108. ```
  109. Output:
  110. ```
  111. <a>
  112. <foo>bar</foo>
  113. <foo2>bar2</foo2>
  114. </a>
  115. ```
  116. #### Example 7: Nested tags with attributes (with indenting)
  117. ```javascript
  118. const xmlOptions = {
  119. header: false,
  120. indent: " ",
  121. };
  122. toXML(
  123. {
  124. ooo: {
  125. _name: "foo",
  126. _attrs: {
  127. a: "b",
  128. },
  129. _content: {
  130. _name: "bar",
  131. _attrs: {
  132. c: "d",
  133. },
  134. },
  135. },
  136. },
  137. xmlOptions
  138. );
  139. ```
  140. Output:
  141. ```
  142. <ooo>
  143. <foo a="b">
  144. <bar c="d"/>
  145. </foo>
  146. </ooo>
  147. ```
  148. Note that cases like this might be especially hard to read because of the deep nesting, so it's recommend you use something like this pattern instead, which breaks it up into more readable pieces:
  149. ```javascript
  150. const bar = {
  151. _name: "bar",
  152. _attrs: {
  153. c: "d",
  154. },
  155. };
  156. const foo = {
  157. _name: "foo",
  158. _attrs: {
  159. a: "b",
  160. },
  161. _content: bar,
  162. };
  163. const xmlOptions = {
  164. header: false,
  165. indent: " ",
  166. };
  167. return toXML(
  168. {
  169. ooo: foo,
  170. },
  171. xmlOptions
  172. );
  173. ```
  174. #### Example 8: Complex functions
  175. Function outputs will be processed (fed back into toXML), meaning that you can output objects that will in turn be converted to XML.
  176. ```javascript
  177. toXML({
  178. someNestedXML: () => {
  179. return {
  180. foo: "bar",
  181. };
  182. },
  183. });
  184. ```
  185. Output:
  186. ```
  187. <someNestedXML><foo>bar</foo></someNestedXML>
  188. ```
  189. #### Example 9: RSS Feed
  190. ```javascript
  191. const xmlOptions = {
  192. header: true,
  193. indent: " ",
  194. };
  195. toXML(
  196. {
  197. _name: "rss",
  198. _attrs: {
  199. version: "2.0",
  200. },
  201. _content: {
  202. channel: [
  203. {
  204. title: "RSS Example",
  205. },
  206. {
  207. description: "Description",
  208. },
  209. {
  210. link: "google.com",
  211. },
  212. {
  213. lastBuildDate: () => new Date(),
  214. },
  215. {
  216. pubDate: () => new Date(),
  217. },
  218. {
  219. language: "en",
  220. },
  221. {
  222. item: {
  223. title: "Item title",
  224. link: "Item link",
  225. description: "Item Description",
  226. pubDate: () => new Date(),
  227. },
  228. },
  229. {
  230. item: {
  231. title: "Item2 title",
  232. link: "Item2 link",
  233. description: "Item2 Description",
  234. pubDate: () => new Date(),
  235. },
  236. },
  237. ],
  238. },
  239. },
  240. xmlOptions
  241. );
  242. ```
  243. Output:
  244. ```
  245. <?xml version="1.0" encoding="UTF-8"?>
  246. <rss version="2.0">
  247. <channel>
  248. <title>RSS Example</title>
  249. <description>Description</description>
  250. <link>google.com</link>
  251. <lastBuildDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</lastBuildDate>
  252. <pubDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</pubDate>
  253. <language>en</language>
  254. <item>
  255. <title>Item title</title>
  256. <link>Item link</link>
  257. <description>Item Description</description>
  258. <pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>
  259. </item>
  260. <item>
  261. <title>Item2 title</title>
  262. <link>Item2 link</link>
  263. <description>Item2 Description</description>
  264. <pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>
  265. </item>
  266. </channel>
  267. </rss>
  268. ```
  269. #### Example 10: Podcast RSS Feed
  270. (see the [Apple docs](http://www.apple.com/itunes/podcasts/specs.html) for more information)
  271. ```javascript
  272. const xmlOptions = {
  273. header: true,
  274. indent: " ",
  275. };
  276. toXML(
  277. {
  278. _name: "rss",
  279. _attrs: {
  280. "xmlns:itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd",
  281. version: "2.0",
  282. },
  283. _content: {
  284. channel: [
  285. {
  286. title: "Title",
  287. },
  288. {
  289. link: "google.com",
  290. },
  291. {
  292. language: "en-us",
  293. },
  294. {
  295. copyright: "Copyright 2011",
  296. },
  297. {
  298. "itunes:subtitle": "Subtitle",
  299. },
  300. {
  301. "itunes:author": "Author",
  302. },
  303. {
  304. "itunes:summary": "Summary",
  305. },
  306. {
  307. description: "Description",
  308. },
  309. {
  310. "itunes:owner": {
  311. "itunes:name": "Name",
  312. "itunes:email": "Email",
  313. },
  314. },
  315. {
  316. _name: "itunes:image",
  317. _attrs: {
  318. href: "image.jpg",
  319. },
  320. },
  321. {
  322. _name: "itunes:category",
  323. _attrs: {
  324. text: "Technology",
  325. },
  326. _content: {
  327. _name: "itunes:category",
  328. _attrs: {
  329. text: "Gadgets",
  330. },
  331. },
  332. },
  333. {
  334. _name: "itunes:category",
  335. _attrs: {
  336. text: "TV &amp; Film",
  337. },
  338. },
  339. {
  340. item: [
  341. {
  342. title: "Podcast Title",
  343. },
  344. {
  345. "itunes:author": "Author",
  346. },
  347. {
  348. "itunes:subtitle": "Subtitle",
  349. },
  350. {
  351. "itunes:summary": "Summary",
  352. },
  353. {
  354. "itunes:image": "image.jpg",
  355. },
  356. {
  357. _name: "enclosure",
  358. _attrs: {
  359. url: "http://example.com/podcast.m4a",
  360. length: "8727310",
  361. type: "audio/x-m4a",
  362. },
  363. },
  364. {
  365. guid: "http://example.com/archive/aae20050615.m4a",
  366. },
  367. {
  368. pubDate: "Wed, 15 Jun 2011 19:00:00 GMT",
  369. },
  370. {
  371. "itunes:duration": "7:04",
  372. },
  373. {
  374. "itunes:keywords": "salt, pepper, shaker, exciting",
  375. },
  376. ],
  377. },
  378. {
  379. item: [
  380. {
  381. title: "Podcast2 Title",
  382. },
  383. {
  384. "itunes:author": "Author2",
  385. },
  386. {
  387. "itunes:subtitle": "Subtitle2",
  388. },
  389. {
  390. "itunes:summary": "Summary2",
  391. },
  392. {
  393. "itunes:image": "image2.jpg",
  394. },
  395. {
  396. _name: "enclosure",
  397. _attrs: {
  398. url: "http://example.com/podcast2.m4a",
  399. length: "655555",
  400. type: "audio/x-m4a",
  401. },
  402. },
  403. {
  404. guid: "http://example.com/archive/aae2.m4a",
  405. },
  406. {
  407. pubDate: "Wed, 15 Jul 2011 19:00:00 GMT",
  408. },
  409. {
  410. "itunes:duration": "11:20",
  411. },
  412. {
  413. "itunes:keywords": "foo, bar",
  414. },
  415. ],
  416. },
  417. ],
  418. },
  419. },
  420. xmlOptions
  421. );
  422. ```
  423. Output:
  424. ```
  425. <?xml version="1.0" encoding="UTF-8"?>
  426. <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  427. <channel>
  428. <title>Title</title>
  429. <link>google.com</link>
  430. <language>en-us</language>
  431. <copyright>Copyright 2011</copyright>
  432. <itunes:subtitle>Subtitle</itunes:subtitle>
  433. <itunes:author>Author</itunes:author>
  434. <itunes:summary>Summary</itunes:summary>
  435. <description>Description</description>
  436. <itunes:owner>
  437. <itunes:name>Name</itunes:name>
  438. <itunes:email>Email</itunes:email>
  439. </itunes:owner>
  440. <itunes:image href="image.jpg"/>
  441. <itunes:category text="Technology">
  442. <itunes:category text="Gadgets"/>
  443. </itunes:category>
  444. <itunes:category text="TV &amp; Film"/>
  445. <item>
  446. <title>Podcast Title</title>
  447. <itunes:author>Author</itunes:author>
  448. <itunes:subtitle>Subtitle</itunes:subtitle>
  449. <itunes:summary>Summary</itunes:summary>
  450. <itunes:image>image.jpg</itunes:image>
  451. <enclosure url="http://example.com/podcast.m4a" length="8727310" type="audio/x-m4a"/>
  452. <guid>http://example.com/archive/aae20050615.m4a</guid>
  453. <pubDate>Wed, 15 Jun 2011 19:00:00 GMT</pubDate>
  454. <itunes:duration>7:04</itunes:duration>
  455. <itunes:keywords>salt, pepper, shaker, exciting</itunes:keywords>
  456. </item>
  457. <item>
  458. <title>Podcast2 Title</title>
  459. <itunes:author>Author2</itunes:author>
  460. <itunes:subtitle>Subtitle2</itunes:subtitle>
  461. <itunes:summary>Summary2</itunes:summary>
  462. <itunes:image>image2.jpg</itunes:image>
  463. <enclosure url="http://example.com/podcast2.m4a" length="655555" type="audio/x-m4a"/>
  464. <guid>http://example.com/archive/aae2.m4a</guid>
  465. <pubDate>Wed, 15 Jul 2011 19:00:00 GMT</pubDate>
  466. <itunes:duration>11:20</itunes:duration>
  467. <itunes:keywords>foo, bar</itunes:keywords>
  468. </item>
  469. </channel>
  470. </rss>
  471. ```
  472. #### Example 11: Custom filter for XML entities, or whatever
  473. ```javascript
  474. const xmlOptions = {
  475. filter: {
  476. "<": "&lt;",
  477. ">": "&gt;",
  478. '"': "&quot;",
  479. "'": "&apos;",
  480. "&": "&amp;",
  481. },
  482. };
  483. toXML(
  484. {
  485. foo: "<a>",
  486. bar: '"b"',
  487. baz: "'&whee'",
  488. },
  489. xmlOptions
  490. );
  491. ```
  492. Output:
  493. ```
  494. <foo>&lt;a&gt;</foo><bar>&quot;b&quot;</bar><baz>&apos;&amp;whee&apos;</baz>
  495. ```
  496. #### Example 11b: Custom filter for XML attributes
  497. ```javascript
  498. const xmlOptions = {
  499. attributesFilter: {
  500. "<": "&lt;",
  501. ">": "&gt;",
  502. '"': "&quot;",
  503. "'": "&apos;",
  504. "&": "&amp;",
  505. },
  506. };
  507. toXML(
  508. {
  509. _name: "foo",
  510. _attrs: { a: '<"\'&"foo>' },
  511. },
  512. xmlOptions
  513. );
  514. ```
  515. Output:
  516. ```
  517. <foo a="&lt;&quot;&apos;&amp;&quot;foo&gt;"/>
  518. ```
  519. #### Example 12: Avoiding self-closing tags
  520. If for some reason you want to avoid self-closing tags, you can pass in a special config option `_selfCloseTag`:
  521. ```javascript
  522. const xmlOptions = {
  523. _selfCloseTag: false,
  524. };
  525. toXML(
  526. {
  527. foo: "",
  528. bar: undefined,
  529. },
  530. xmlOptions
  531. );
  532. ```
  533. Output:
  534. ```
  535. <foo></foo><bar>whee</bar>
  536. ```
  537. #### Example 13: Custom XML header
  538. ```javascript
  539. const xmlOptions = {
  540. header: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?>',
  541. };
  542. toXML(
  543. {
  544. foo: "bar",
  545. },
  546. xmlOptions
  547. );
  548. ```
  549. Output:
  550. ```
  551. <?xml version="1.0" encoding="UTF-16" standalone="yes"?><foo>bar</foo><foo2>bar2</foo2>
  552. ```
  553. #### Example 14: Emoji attribute support (needed for AMP)
  554. ```javascript
  555. toXML({
  556. html: {
  557. _attrs: {
  558. "⚡": true,
  559. },
  560. },
  561. });
  562. ```
  563. Output:
  564. ```
  565. <html />
  566. ```
  567. #### Example 15: Duplicate attribute key support
  568. ```javascript
  569. toXML({
  570. html: {
  571. _attrs: [
  572. {
  573. lang: "en",
  574. },
  575. {
  576. lang: "klingon",
  577. },
  578. ],
  579. },
  580. });
  581. ```
  582. Output:
  583. ```
  584. <html lang="en" lang="klingon"/>
  585. ```
  586. #### Example 16: XML comments
  587. ```javascript
  588. toXML({
  589. _comment: 'Some important comment',
  590. a: {
  591. b: [1, 2, 3]
  592. }
  593. }, { indent: ' ' });
  594. ```
  595. Output:
  596. ```
  597. <!-- Some important comment -->
  598. <a>
  599. <b>1</b>
  600. <b>2</b>
  601. <b>3</b>
  602. </a>
  603. ```
  604. #### Example 17: Multiple XML comments
  605. ```javascript
  606. toXML([
  607. { _comment: 'Some important comment' },
  608. { _comment: 'This is a very long comment!' },
  609. { _comment: 'More important exposition!' },
  610. { a: { b: [1, 2, 3] } }
  611. ], { indent: ' ' });
  612. ```
  613. Output:
  614. ```
  615. <!-- Some important comment -->
  616. <!-- This is a very long comment! -->
  617. <!-- More important exposition! -->
  618. <a>
  619. <b>1</b>
  620. <b>2</b>
  621. <b>3</b>
  622. </a>
  623. ```
  624. ### License
  625. MIT