Skip to content

A simple and tiny XML parser and stringifier.

License

Notifications You must be signed in to change notification settings

fabiospampinato/xml-simple-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XML Simple Parser

A simple and tiny XML parser and stringifier.

Install

npm install --save xml-simple-parser

Usage

import XML from 'xml-simple-parser';

// It can parse an XML string

const svg = `
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
  <rect x="11" y="4" width="2" height="16"/>
</svg>
`;

const ast = XML.parse ( svg );
// {
//   "type": "root",
//   "children": [
//     {
//       "type": "prolog",
//       "attributes": {
//         "version": "1.0",
//         "encoding": "UTF-8",
//         "standalone": "no"
//       }
//     },
//     {
//       "type": "doctype",
//       "value": "svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"",
//       "values": [
//         "svg",
//         "PUBLIC",
//         "-//W3C//DTD SVG 1.1//EN",
//         "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"
//       ]
//     },
//     {
//       "type": "element",
//       "name": "svg",
//       "attributes": {
//         "width": "100%",
//         "height": "100%",
//         "viewBox": "0 0 24 24",
//         "version": "1.1",
//         "xmlns": "http://www.w3.org/2000/svg",
//         "xmlns:xlink": "http://www.w3.org/1999/xlink",
//         "xml:space": "preserve"
//       },
//       "children": [
//         {
//           "type": "text",
//           "value": "\n  "
//         },
//         {
//           "type": "element",
//           "name": "rect",
//           "attributes": {
//             "x": "11",
//             "y": "4",
//             "width": "2",
//             "height": "16"
//           },
//           "children": [],
//           "void": true
//         },
//         {
//           "type": "text",
//           "value": "\n"
//         }
//       ],
//       "void": false
//     },
//     {
//       "type": "text",
//       "value": "\n"
//     }
//   ]
// }

// It can stringify an AST back to an equivalent XML string

const xml = XML.stringify ( ast );
// <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
// <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
// <svg width="100%" height="100%" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
//   <rect x="11" y="4" width="2" height="16"/>
// </svg>

// It can check if a string is a valid XML string, according to this parser anyway

validate ( svg ); // => true
validate ( xml ); // => true

License

MIT © Fabio Spampinato