{"id":23120492,"url":"https://github.com/typescriptlibs/tsl-core-xml","last_synced_at":"2025-07-03T06:06:33.047Z","repository":{"id":176761868,"uuid":"656540871","full_name":"typescriptlibs/tsl-core-xml","owner":"typescriptlibs","description":"TypeScript Library for XML","archived":false,"fork":false,"pushed_at":"2025-03-14T20:02:06.000Z","size":191,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-05-27T10:45:47.308Z","etag":null,"topics":["html","typescript","xml"],"latest_commit_sha":null,"homepage":"https://typescriptlibs.org/tsl-core-xml/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typescriptlibs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.md","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-06-21T06:46:26.000Z","updated_at":"2025-03-14T20:02:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"f232125a-1314-4623-9507-725fc654b944","html_url":"https://github.com/typescriptlibs/tsl-core-xml","commit_stats":null,"previous_names":["typescriptlibs/tsl-core-xml"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescriptlibs%2Ftsl-core-xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescriptlibs%2Ftsl-core-xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescriptlibs%2Ftsl-core-xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescriptlibs%2Ftsl-core-xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typescriptlibs","download_url":"https://codeload.github.com/typescriptlibs/tsl-core-xml/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescriptlibs%2Ftsl-core-xml/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":257710938,"owners_count":22589629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html","typescript","xml"],"created_at":"2024-12-17T06:11:14.498Z","updated_at":"2025-07-03T06:06:33.024Z","avatar_url":"https://github.com/typescriptlibs.png","language":"TypeScript","readme":"XML TypeScript Library\n======================\n\nThis package provides simple ways to parse any XML-like text.\n\n\n\n[![CodeQL](https://github.com/typescriptlibs/tsl-core-xml/workflows/CodeQL/badge.svg)](https://github.com/typescriptlibs/tsl-core-xml/actions/workflows/codeql.yml)\n[![Node.js](https://github.com/typescriptlibs/tsl-core-xml/workflows/Node.js/badge.svg)](https://github.com/typescriptlibs/tsl-core-xml/actions/workflows/node.js.yml)\n[![NPM](https://img.shields.io/npm/v/tsl-core-xml.svg)](https://www.npmjs.com/package/tsl-core-xml)\n[![License](https://img.shields.io/npm/l/tsl-core-xml.svg)](https://github.com/typescriptlibs/tsl-core-xml/blob/main/LICENSE.md)\n\n\n\nTable of Content\n----------------\n\n- [XMLTree](#xmltree)\n- [XMLTree Example](#xmltree-example)\n- [XMLScanner](#xmlscanner)\n- [XMLScanner Example](#xmlscanner-example)\n- [Types of XML Nodes](#types-of-xml-nodes)\n- [XMLPrinter Example](#xmlprinter-example)\n\n\n\nXMLTree\n-------\n\n\nThe XML tree, often also called the DOM (Document Object Model), is the natural\nrepresentation of XML. The `XMLTree` class can have multiple root nodes in the\n`XMLTree.roots` property.\n\nUsually the last root is the one that contains most data. Or you check each root\nif it is a tag by using the `isXMLTag` helper function. Afterwards you can check\nthe `XMLTag.innerXML` property for child nodes.\n\nYou can also use the `XMLTree.query` function to extract XML nodes with the help\nof selectors as known from CSS. It depends on the selector and use case whether\nthis is faster than a custom walk through the tree nodes.\n\nThe `XMLTree` uses the `XMLScanner`, which is available via the\n`XMLTree.scanner` property. There you can adjust the `XMLScanner.cdataTags`\nproperty or the `XMLScanner.scanSize` property for special use cases.\n\n\n\nXMLTree Example\n---------------\n\n``` TypeScript\nconst tree = XMLTree.parse(\n    '\u003c!DOCTYPE html\u003e' +\n    '\u003chtml lang=\"en\"\u003e\u003chead\u003e\u003ctitle\u003eMy Webpage\u003c/title\u003e\u003c/head\u003e' +\n    '\u003cbody style=\"background:#9CF\"\u003e\u003ch1\u003eMy Webpage\u003c/h1\u003e\u003chr /\u003e\u003c/body\u003e\u003c/html\u003e'\n);\n\nconsole.log( JSON.stringify( tree.query('body h1') );\nconsole.log( JSON.stringify( tree.roots, null, '  ' ) );\n```\n``` JSON\n{\n  \"tag\": \"h1\",\n  \"innerXML\": [\n    \"My Webpage\"\n  ]\n}\n```\n``` JSON\n[{\n  \"tag\": \"!DOCTYPE\",\n  \"attributes\": {\n    \"html\": \"\"\n  }\n}, {\n  \"tag\": \"html\",\n  \"attributes\": {\n    \"lang\": \"en\"\n  }\n  \"innerXML\": [{\n    \"tag\": \"head\",\n    \"innerXML\": [{\n      \"tag\": \"title\",\n      \"innerXML\": [\n        \"My Webpage\"\n      ]\n    }]\n  }, {\n    \"tag\": \"body\",\n    \"attributes\": {\n      \"style\": \"background:#9CF\"\n    },\n    \"innerXML\": [{\n      \"tag\": \"h1\",\n      \"innerXML\": [\n        \"My Webpage\"\n      ]\n    }, {\n      \"tag\": \"hr\",\n      \"empty\": true\n    }]\n  }]\n}]\n```\n\n\n\nXMLScanner\n----------\n\nIf XML should be read exactly like it is written, the `XMLScanner` is the class\nto use. It keeps every linebreak and every variant of a closing tag. The only\nthings not preserved by the scanner are the surrounding quote characters for\nattribute values.\n\nIf you expect text between XML tags or an XML tag itself to be larger than 1 MB,\nthen you should increase the value of the `XMLScanner.scanSize` property\naccordingly. If you like to save memory during a scan, you can also decrease the\nscan size.\n\n\n\nXMLScanner Example\n------------------\n\n``` TypeScript\nconst scanner = new XMLScanner(\n    '\u003c!DOCTYPE html\u003e' +\n    '\u003chtml lang=\"en\"\u003e\u003chead\u003e\u003ctitle\u003eMy Webpage\u003c/title\u003e\u003c/head\u003e' +\n    '\u003cbody style=\"background:#9CF\"\u003e\u003ch1\u003eMy Webpage\u003c/h1\u003e\u003chr /\u003e\u003c/body\u003e\u003c/html\u003e'\n);\n\nlet node: ( XMLNode | undefined );\n\nwhile ( node = scanner.scan() ) {\n    console.log( node );\n}\n```\n``` JavaScript\n{ tag: \"!DOCTYPE\", attributes: { html: \"\" } }\n{ tag: \"html\", attributes: { lang: \"en\" } }\n{ tag: \"head\" }\n{ tag: \"title\" }\n\"My Webpage\"\n{ tag: \"/title\" }\n{ tag: \"/head\" }\n{ tag: \"body\", attributes: { style: \"background:#9CF\" } }\n{ tag: \"h1\" }\n\"My Webpage\"\n{ tag: \"/h1\" }\n{ tag: \"hr\", empty: true }\n{ tag: \"/body\" }\n{ tag: \"/html\" }\n```\n\n\n\nTypes of XML Nodes\n------------------\n\nXML has 7 different types of nodes.\n\n- `string`: Text strings are the regular escaped text between tags. Use the\n  `isString` helper function to test for this node type.\n\n- `XMLCdata`: Character data is unescaped text, that contains raw data like\n  JavaScript. Use the `isXMLCdata` helper function to test for this node type.\n\n- `XMLComment`: A comment is a special form of tag, that has not impact on the\n  content of the XML. Use the `isXMLComment` helper function to test for this\n  node type.\n\n- `XMLTag`: Tags are nodes with a name and attributes. Use the `isXMLTag` helper\n  function to test for this node type. There exists 4 subtypes of tags.\n\n  - Empty Tag: This tag is self-closing and has the `XMLTag.empty` property set\n    to `true`. Typical empty tags are `img`, `meta`, and `path`.\n\n  - Regular Tag: This tag often contains child nodes in the `XMLTag.innerXML`\n    property. Typical regular tags are `a`, `p`, and `text`.\n\n  - Document Type Definition: This tag is similar to a regular tag, but has a\n    name starting with a `!` character. A typical definition tag is `!DOCTYPE`.\n\n  - Processing Instruction: This tag is similar to an empty tag, but has a name\n    starting with a `?` character. A typical instruction tag is `?xml`.\n\n\n\n\nXMLPrinter Example\n------------------\n\n``` TypeScript\nconst printer = new XMLPrinter( [\n    { tag: \"!DOCTYPE\", attributes: { html: \"\" } },\n    { tag: \"html\", attributes: { lang: \"en\" } }\n]);\n\nconsole.log( printer.toString() );\n```\n``` HTML\n\u003c!DOCTYPE html=\"\"\u003e\u003chtml\u003e\u003c/html\u003e\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypescriptlibs%2Ftsl-core-xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypescriptlibs%2Ftsl-core-xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypescriptlibs%2Ftsl-core-xml/lists"}