{"name":"xmldoc","version":"2.0.1","keywords":["xml","sax","parser","xpath","document"],"author":{"url":"http://nfarina.com","name":"Nick Farina","email":"nfarina@gmail.com"},"license":"MIT","_id":"xmldoc@2.0.1","maintainers":[{"name":"nfarina","email":"nfarina@gmail.com"}],"contributors":[{"name":"Nick Farina","email":"nfarina@gmail.com"},{"name":"Caleb Meredith","email":"calebmeredith8@gmail.com"}],"homepage":"https://github.com/nfarina/xmldoc#readme","bugs":{"url":"https://github.com/nfarina/xmldoc/issues"},"dist":{"shasum":"a901f6a6341e4d8cba3dbc5fc61017249f2adf24","tarball":"http://registry-npm.ywwl.com/xmldoc/download/xmldoc-2.0.1.tgz","fileCount":10,"integrity":"sha512-sOOqgsjl3PU6iBw+fBUGAkTCE+JFK+sBaOL3pnZgzqk2/yvOD7RlFmZtDRJAEBzdpOYxSXyOQH4mjubdfs3MSg==","signatures":[{"sig":"MEQCIFdkMSu+ep1hW6ScLGQ2QUYEwAiCm96+Yrpuj+ipByKGAiAq0Aip261IgYhg2rA4zFbxbUvhnSmQnb/RkbWGOqulqg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":61921,"size":15310,"noattachment":false,"key":"/xmldoc/-/xmldoc-2.0.1.tgz"},"main":"./lib/xmldoc.js","type":"commonjs","types":"./dist/xmldoc.d.ts","engines":{"node":">=12.0.0"},"exports":{".":{"types":"./dist/xmldoc.d.ts","import":"./dist/xmldoc.js","default":"./lib/xmldoc.js","require":"./lib/xmldoc.js"}},"gitHead":"78911d03f0b528e9b7cf1680f86152a528710c0d","scripts":{"test":"yarn test:js || true && yarn test:ts","build":"tsc --sourceMap","clean":"rm -rf dist","format":"prettier --write .","test:js":"tap test/*.js","test:ts":"vitest run test/ts","coverage":"npm test:js -- --cov --coverage-report=html","prepublishOnly":"npm run clean && npm run build"},"_npmUser":{"name":"nfarina","email":"nfarina@gmail.com"},"repository":{"url":"git://github.com/nfarina/xmldoc.git","type":"git"},"_npmVersion":"11.3.0","description":"A lightweight XML Document class for JavaScript.","directories":{},"_nodeVersion":"22.14.0","dependencies":{"sax":"^1.2.4"},"_hasShrinkwrap":false,"packageManager":"yarn@4.7.0","devDependencies":{"tap":"^16.3.0","vitest":"^3.1.3","prettier":"^2.7.1","@types/sax":"^1.2.7","typescript":"^5.8.2"},"_npmOperationalInternal":{"tmp":"tmp/xmldoc_2.0.1_1746552733419_0.6088028383160269","host":"s3://npm-registry-packages-npm-production"},"readme":"[![CI](https://github.com/nfarina/xmldoc/actions/workflows/ci.yml/badge.svg)](https://github.com/nfarina/xmldoc/actions/workflows/ci.yml)\n\n## Introduction\n\n`xmldoc` lets you parse XML documents with ease. It's a lightweight XML document class with a single dependency on the excellent [`sax`][sax] parser.\n\nFor more on why I wrote this class, see the [blog post][blog].\n\n`xmldoc` is written in TypeScript and ships full type definitions. As of version 3.0 it is published as an ESM-only package (`import` only); if you need CommonJS `require()` support, stay on the 2.x line.\n\n[blog]: http://nfarina.com/post/34302964969/a-lightweight-xml-document-class-for-nodejs-javascript\n\n## Release Notes\n\nSee [CHANGELOG.md](./CHANGELOG.md) for details.\n\n## Installation\n\n```bash\nnpm install xmldoc\n# or\nyarn add xmldoc\n```\n\n## Usage\n\n```ts\nimport { XmlDocument } from \"xmldoc\";\n\nconst document = new XmlDocument(\"<some>xml</some>\");\n```\n\n### React Native\n\nIf you're using React Native, you may need to install `buffer` and `stream` separately:\n\n```bash\nnpm install buffer stream xmldoc\n```\n\n## Classes\n\nThe primary exported class is `XmlDocument`, which you'll use to consume your XML text. `XmlDocument` contains a hierarchy of `XmlElement` instances representing the XML structure.\n\nBoth `XmlElement` and `XmlDocument` contain the same members and methods you can call to traverse the document or a subtree.\n\n## Members\n\n| Member name                                      | Default empty value | Description                                                                                                                                                                      |\n| :----------------------------------------------- | :------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `name`                                           |                     | Node name, like \"tat\" for `<tat>`. XML \"namespaces\" are ignored by the underlying [sax-js](https://github.com/isaacs/sax-js) parser: so `<office:body>` becomes `\"office:body\"`. |\n| `attr`                                           | `{}`                | Object dict containing attribute properties. Like `bookNode.attr.title` for `<book title=\"...\">`.                                                                                |\n| `val`                                            | `\"\"`                | String \"value\" of the node, if any. Like `\"world\"` for `<hello>world</hello>`.                                                                                                   |\n| `children`                                       | `[]`                | Array of `XmlElement` children of the node.                                                                                                                                      |\n| `firstChild`                                     |                     | What it sounds like. `null` if no children.                                                                                                                                      |\n| `lastChild`                                      |                     | What it sounds like. `null` if no children.                                                                                                                                      |\n| `line`, `column`, `position`, `startTagPosition` |                     | Information about the element's original position in the XML string.                                                                                                             |\n\n## Methods\n\nAll methods with `child` in the name operate only on direct children; they do not do a deep/recursive search.\n\nIt's important to note that `xmldoc` is designed for when you know exactly what you want from your XML file. For instance, it's great for parsing API responses with known structures, but it's not great at teasing things out of HTML documents from the web.\n\nIf you need to do lots of searching through your XML document, I highly recommend trying a different library like [node-elementtree](https://github.com/racker/node-elementtree).\n\n### eachChild(func)\n\nSimilar to [underscore's][underscore] `each` method, it will call `func(child, index, array)` for each child of the given node.\n\n### childNamed(name)\n\nPass it the name of a child node and it will search for and return the first one found, or `undefined`.\n\n### childrenNamed(name)\n\nLike `childNamed` but returns all matching children in an array, or `[]`.\n\n### childWithAttribute(name,value)\n\nSearches for the first child with the given attribute value. You can omit `value` to just find the first node with the given attribute defined at all.\n\n### descendantWithPath(path)\n\nSearches for a specific \"path\" using dot notation. Example:\n\n```xml\n<book>\n  <author>\n    <name isProper=\"true\">George R. R. Martin</name>\n    ...\n  </author>\n  ...\n</book>\n```\n\nIf you just want the `<name>` node and you have the `XmlElement` for the `<book>` node, you can say:\n\n```js\nconst nameNode = bookNode.descendantWithPath(\"author.name\"); // return <name> node\n```\n\n### valueWithPath(path)\n\nJust like `descendantWithPath`, but goes deeper and extracts the `val` of the node. Example:\n\n```js\nconst authorName = bookNode.valueWithPath(\"author.name\"); // return \"George R. R. Martin\"\n```\n\nYou can also use the `@` character to request the value of a particular _attribute_ instead:\n\n```js\nconst authorIsProper = bookNode.valueWithPath(\"author.name@isProper\"); // return \"true\"\n```\n\nThis is not [XPath][]! It's just a thing I made up, OK?\n\n### toString([options])\n\nThis is just an override of the standard JavaScript method, it will give you a string representation of your XML document or element. Note that this is for debugging only! It is not guaranteed to always output valid XML.\n\nThe default implementation of `toString()`, that is, the one you get when you just `console.log(\"Doc: \" + myDoc)` will pretty-print the XML with linebreaks and indents. You can pass a couple options to control the output:\n\n```js\nxml.toString({ compressed: true }); // strips indents and linebreaks\nxml.toString({ trimmed: true }); // trims long strings for easier debugging\nxml.toString({ preserveWhitespace: true }); // prevents whitespace from being removed from around element values\nxml.toString({ html: true }); // uses HTML self-closing tag rules for elements without children\n```\n\nPutting it all together:\n\n```js\nconst xml =\n  \"<author><name>Lorem ipsum dolor sit amet, consectetur</name></author>\";\nconsole.log(\n  \"My document: \\n\" + new XmlDocument(xml).toString({ trimmed: true }),\n);\n```\n\nPrints:\n\n    My document:\n    <author>\n      <name>Lorem ipsum dolor sit ame…</name>\n    </author>\n\n## Feedback\n\nFeel free to file issues or hit me up on [X][x].\n\n[underscore]: http://underscorejs.org\n[XPath]: http://en.wikipedia.org/wiki/XPath\n[x]: http://twitter.com/nfarina\n[sax]: https://github.com/isaacs/sax-js\n","publish_time":1746552733637,"_cnpm_publish_time":1746552733637,"dist-tags":{"beta":"2.0.0-beta.3","latest":"3.0.0"}}