{"id":26021210,"url":"https://github.com/teclone/xml-serializer","last_synced_at":"2025-03-06T08:50:31.045Z","repository":{"id":38174867,"uuid":"140106854","full_name":"teclone/xml-serializer","owner":"teclone","description":"xml-serializer is a complete JavaScript implementation of the W3C xml serialization specifications","archived":false,"fork":false,"pushed_at":"2022-12-07T20:43:48.000Z","size":5146,"stargazers_count":7,"open_issues_count":23,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-24T01:18:01.823Z","etag":null,"topics":["dom-parser","jsdom","npm","w3c-specification","xml-serialization","xml-serializer"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/teclone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-07T18:06:26.000Z","updated_at":"2020-03-02T19:20:40.000Z","dependencies_parsed_at":"2023-01-24T22:15:32.819Z","dependency_job_id":null,"html_url":"https://github.com/teclone/xml-serializer","commit_stats":null,"previous_names":["harrison-ifeanyichukwu/xml-serializer"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teclone%2Fxml-serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teclone%2Fxml-serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teclone%2Fxml-serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teclone%2Fxml-serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teclone","download_url":"https://codeload.github.com/teclone/xml-serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242179280,"owners_count":20084940,"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":["dom-parser","jsdom","npm","w3c-specification","xml-serialization","xml-serializer"],"created_at":"2025-03-06T08:50:30.252Z","updated_at":"2025-03-06T08:50:31.019Z","avatar_url":"https://github.com/teclone.png","language":"JavaScript","readme":"# XML-Serializer\n\n[![Build Status](https://travis-ci.org/teclone/xml-serializer.svg?branch=master)](https://travis-ci.org/teclone/xml-serializer)\n[![Coverage Status](https://coveralls.io/repos/github/teclone/xml-serializer/badge.svg?branch=master)](https://coveralls.io/github/teclone/xml-serializer?branch=master)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![npm version](https://badge.fury.io/js/%40teclone%2Fxml-serializer.svg)](https://badge.fury.io/js/%40teclone%2Fxml-serializer)\n![npm](https://img.shields.io/npm/dt/%40teclone%2Fxml-serializer.svg)\n\nXML-Serializer is a complete JavaScript implementation of the W3C [xml serialization](https://www.w3.org/TR/DOM-Parsing/#dfn-concept-serialize-xml) specifications. All specifications have been implemented and includes the following [specs](https://www.w3.org/TR/DOM-Parsing/#dfn-concept-xml-serialization-algorithm):\n\n- [ELEMENT_NODE Serialization]\n\n- [DOCUMENT_NODE Serialization]\n\n- [COMMENT_NODE Serialization]\n\n- [TEXT_NODE Serialization]\n\n- [DOCUMENT_FRAGMENT_NODE Serialization]\n\n- [DOCUMENT_TYPE_NODE Serialization]\n\n- [PROCESSING_INSTRUCTION_NODE Serialization]\n\n## Module Availability\n\nThis module is available as an [npm](https://www.npmjs.com/package/@teclone/xml-serializer) scoped package and also has a browser build that is located inside the `dist` folder. It can easily be integrated with [JSDOM](https://github.com/jsdom/jsdom) for mockup testing.\n\n## Getting Started\n\nThe below command will install `xml-serializer` from npm into your project assuming you have the [npm](https://www.npmjs.com/) already installed.\n\n**Install as a development dependency**:\n\n```bash\nnpm install --save-dev @teclone/xml-serializer\n```\n\n## Usage Guide\n\nFollowing the specification, the `XMLSerializer` interface is a constructor and has a `serializeToString(root)` method exposed on the instance. To serialize any xml node, call the `serializeToString(root)` method on a constructed instance, passing in the xml node as like shown below:\n\n```javascript\nimport XMLSerializer from '@teclone/xml-serializer';\n\nconst instance = new XMLSerializer();\nconsole.log(instance.serializeToString(someXmlNode));\n```\n\nThe constructor can take a boolean argument that indicates if whitespace should be preserved in the serialized output. Default value is `true`;\n\n```javascript\n// do not preserve white space\nconst instance = new XMLSerializer(false);\nconst xmlString = instance.serializeToString(document);\n```\n\n### Using with [JSDOM](https://github.com/jsdom/jsdom)\n\nCurrently [at the time of creating this], JSDOM has not implemented the `XMLSerializer` interface. This can be easily integrated with JSDOM and any other similar mockup environment or for web scrapping and xml feed parsing like below.\n\n```javascript\n//assumes jsdom has been installed.\nimport XMLSerializer from '@teclone/xml-serializer';\nimport { JSDOM } from 'jsdom';\n\nconst dom = new JSDOM();\nXMLSerializer.installTo(dom.window);\n\nglobal.window = dom.window;\n\n//start running your tests or do something else.\n```\n\n### Using on the browser\n\nThe browser build is available inside the `build/dist` folder when you npm install the package. You can also clone this repo and run the build command locally. It exposes an `XMLSerializer` construct on the `window` object.\n\n```html\n\u003cscript\n  type=\"text/javascript\"\n  src=\"node_modules/@teclone/xml-serializer/build/dist/main.js\"\n\u003e\n  \u003cscript\u003e\n  \u003cscript type=\"text/javascript\"\u003e\n      const serializer = new XMLSerializer();\n      // do some serialization stuffs\n\u003c/script\u003e\n```\n\n## Features \u0026 Improvements\n\nBy default, the serializer preserves white space during the serialization process. This can be turned off if you want a compact output by passing in `false` to the constructor at the time of creating an instance.\n\n```javascript\n//do not preserve white space\nconst instance = new XMLSerializer(false);\n```\n\nAnother improvement is that it removes all duplicate xml prefix definition on as recommended in the specification document unlike what web browsers do. Below is an example of\nthis:\n\n**Original XML**:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\n\u003c!DOCTYPE root PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"\u003e\n\n\u003c?xml-stylesheet href=\"classic.css\" alternate=\"yes\" title=\"Classic\"\n media=\"screen, print\" type=\"text/css\"?\u003e\n\n\u003c!--notice that two namespaces have been defined on the root element--\u003e\n\u003croot xmlns:h=\"http://www.w3.org/TR/html4/\" xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n\n    \u003c!--notice that it is declared again here. this is a duplicate--\u003e\n    \u003ch:table xmlns:h=\"http://www.w3.org/TR/html4/\" xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n        \u003ch:tr\u003e\n            \u003ch:td\u003e\n            \u003ch:td\u003eApples\u003c/h:td\u003e\n            \u003ch:td\u003eBananas\u003c/h:td\u003e\n        \u003c/h:tr\u003e\n    \u003c/h:table\u003e\n\n    \u003c!--one is duplicated here--\u003e\n    \u003cf:table xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n        \u003cf:name\u003eAfrican Coffee Table\u003c/f:name\u003e\n        \u003cf:width\u003e80\u003c/f:width\u003e\n        \u003cf:length\u003e120\u003c/f:length\u003e\n    \u003c/f:table\u003e\n\n    \u003c!--html section--\u003e\n    \u003chtml xmlns=\"http://www.w3.org/1999/xhtml\"\u003e\n        \u003chead\u003e\n            \u003cmeta name=\"description\" content=\"this is html section\" /\u003e\n            \u003cbase href=\"http://localhost\" /\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003cp\u003ethis is a paragraph text\u003c/p\u003e\n            \u003chr /\u003e\n            \u003ctemplate\u003e\n                \u003cp\u003ethis is a template\u003c/p\u003e\n            \u003c/template\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n\n    \u003csvg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"\u003e\n        \u003csvg:style\u003e\u003c/svg:style\u003e\n        \u003ctitle\u003emy title\u003ctitle\u003e\n    \u003c/svg:svg\u003e\n\n\u003c/root\u003e\n```\n\n**Chrome inbuilt XMLSerializer Output:**\n\nNotice that none of the duplicated namespaces is removed.\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\u003c!DOCTYPE root PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"\u003e\n\n\u003c?xml-stylesheet href=\"classic.css\" alternate=\"yes\" title=\"Classic\"\n media=\"screen, print\" type=\"text/css\"?\u003e\n\n\u003croot xmlns:h=\"http://www.w3.org/TR/html4/\" xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n\n    \u003c!-- duplicates still remains --\u003e\n    \u003ch:table xmlns:h=\"http://www.w3.org/TR/html4/\" xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n        \u003ch:tr\u003e\n            \u003ch:td\u003e\n            \u003ch:td\u003eApples\u003c/h:td\u003e\n            \u003ch:td\u003eBananas\u003c/h:td\u003e\n        \u003c/h:tr\u003e\n    \u003c/h:table\u003e\n\n    \u003c!--still remains--\u003e\n    \u003cf:table xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n        \u003cf:name\u003eAfrican Coffee Table\u003c/f:name\u003e\n        \u003cf:width\u003e80\u003c/f:width\u003e\n        \u003cf:length\u003e120\u003c/f:length\u003e\n    \u003c/f:table\u003e\n\n    \u003c!--html section--\u003e\n    \u003chtml xmlns=\"http://www.w3.org/1999/xhtml\"\u003e\n        \u003chead\u003e\n            \u003cmeta name=\"description\" content=\"this is html section\" /\u003e\n            \u003cbase href=\"http://localhost\" /\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003cp\u003ethis is a paragraph text\u003c/p\u003e\n            \u003chr /\u003e\n            \u003ctemplate\u003e\n                \u003cp\u003ethis is a template\u003c/p\u003e\n            \u003c/template\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n\n    \u003csvg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"\u003e\n        \u003csvg:style\u003e\u003c/svg:style\u003e\n        \u003ctitle\u003emy title\u003ctitle\u003e\n    \u003c/svg:svg\u003e\n\n\u003c/root\u003e\n```\n\n**Output of this module:**\n\nNotice that all of the duplicated namespaces are removed.\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\u003c!DOCTYPE root PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"\u003e\n\n\u003c?xml-stylesheet href=\"classic.css\" alternate=\"yes\" title=\"Classic\"\n media=\"screen, print\" type=\"text/css\"?\u003e\n\n\u003c!--notice that two namespaces have been defined on the root element--\u003e\n\u003croot xmlns:h=\"http://www.w3.org/TR/html4/\" xmlns:f=\"https://www.w3schools.com/furniture\"\u003e\n\n    \u003c!--duplicates removed--\u003e\n    \u003ch:table\u003e\n        \u003ch:tr\u003e\n            \u003ch:td\u003e\n            \u003ch:td\u003eApples\u003c/h:td\u003e\n            \u003ch:td\u003eBananas\u003c/h:td\u003e\n        \u003c/h:tr\u003e\n    \u003c/h:table\u003e\n\n    \u003c!--duplicate removed--\u003e\n    \u003cf:table\u003e\n        \u003cf:name\u003eAfrican Coffee Table\u003c/f:name\u003e\n        \u003cf:width\u003e80\u003c/f:width\u003e\n        \u003cf:length\u003e120\u003c/f:length\u003e\n    \u003c/f:table\u003e\n\n    \u003c!--html section--\u003e\n    \u003chtml xmlns=\"http://www.w3.org/1999/xhtml\"\u003e\n        \u003chead\u003e\n            \u003cmeta name=\"description\" content=\"this is html section\" /\u003e\n            \u003cbase href=\"http://localhost\" /\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003cp\u003ethis is a paragraph text\u003c/p\u003e\n            \u003chr /\u003e\n            \u003ctemplate\u003e\n                \u003cp\u003ethis is a template\u003c/p\u003e\n            \u003c/template\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n\n    \u003csvg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"\u003e\n        \u003csvg:style\u003e\u003c/svg:style\u003e\n        \u003ctitle\u003emy title\u003ctitle\u003e\n    \u003c/svg:svg\u003e\n\n\u003c/root\u003e\n```\n\n## Contributing\n\nWe welcome your own contributions, ranging from code refactoring, documentation improvements, new feature implementations, bugs/issues reporting, etc. We recommend you follow the steps below to actively contribute to this project:\n\n1. Decide on what to help us with.\n\n2. Fork this repo to your machine.\n\n3. Implement your ideas, and once stable,\n\n4. Create a pull request, explaining your improvements/features\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteclone%2Fxml-serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteclone%2Fxml-serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteclone%2Fxml-serializer/lists"}