{"id":16690273,"url":"https://github.com/ElyaConrad/XML-Parser","last_synced_at":"2025-03-21T18:33:40.769Z","repository":{"id":18940322,"uuid":"85629941","full_name":"ElyaConrad/XML-Parser","owner":"ElyaConrad","description":"A Node.js XML DOM, Parser \u0026 Stringifier.","archived":false,"fork":false,"pushed_at":"2022-04-19T14:42:42.000Z","size":19,"stargazers_count":18,"open_issues_count":6,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-05T09:33:14.927Z","etag":null,"topics":["crawler","crawling","dom","html","html-parser","html-parsing","xml","xml-parser","xml-parsing","xml-schema"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ElyaConrad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-20T21:38:07.000Z","updated_at":"2024-10-09T07:34:42.000Z","dependencies_parsed_at":"2022-08-07T09:01:02.064Z","dependency_job_id":null,"html_url":"https://github.com/ElyaConrad/XML-Parser","commit_stats":null,"previous_names":["elyaconrad/xml-parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElyaConrad%2FXML-Parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElyaConrad%2FXML-Parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElyaConrad%2FXML-Parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElyaConrad%2FXML-Parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ElyaConrad","download_url":"https://codeload.github.com/ElyaConrad/XML-Parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243818205,"owners_count":20352629,"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":["crawler","crawling","dom","html","html-parser","html-parsing","xml","xml-parser","xml-parsing","xml-schema"],"created_at":"2024-10-12T15:50:57.040Z","updated_at":"2025-03-21T18:33:40.468Z","avatar_url":"https://github.com/ElyaConrad.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XML Parser, Stringifier and DOM\n\nParse XML, HTML and more with a very tolerant XML parser and convert it into a DOM.\n\nThese three components are separated from each other as own modules.\n\n|Component|Size|\n|---|---|\n|Parser|4.7 KB|\n|Stringifier|1.3 KB|\n|DOM|3.1 KB|\n\n## Install\n```bash\nnpm install xml-parse\n```\n## Require\n```javascript\nconst xml = require(\"xml-parse\");\n```\n\n## Parser\n\nParsing is very simple.\n\nJust call the ```parse``` method of the *xml-parse* instance.\n\n```javascript\nconst xml = require(\"xml-parse\");\n\n// Valid XML string\nvar parsedXML = xml.parse('\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e' +\n                          '\u003croot\u003eRoot Element\u003c/root\u003e');\nconsole.log(parsedXML);\n\n// Invalid XML string\nvar parsedInavlidXML = xml.parse('\u003croot\u003e\u003c/root\u003e' +\n                                 '\u003csecondRoot\u003e' +\n                                   '\u003cnotClosedTag\u003e' +\n                                 '\u003c/secondRoot\u003e');\nconsole.log(parsedInavlidXML);\n```\n\n### Parsed Object Structure\n\nThe result of ```parse``` is an object that maybe looks like this:\n\n(In this case we have the xml string of the given example)\n```javascript\n[\n  {\n    type: 'element',\n    tagName: '?xml',\n    attributes: {\n      version: '1.0',\n      encoding: 'UTF-8'\n    },\n    childNodes: [],\n    innerXML: '\u003e',\n    closing: false,\n    closingChar: '?'\n  },\n  {\n    type: 'element',\n    tagName: 'root',\n    attributes: {},\n    childNodes: [\n      {\n        type: 'text',\n        text: 'Root Element'\n      }\n    ],\n    innerXML: 'Root Element',\n    closing: true,\n    closingChar: null\n  }\n]\n```\n**The root object is always an array because of the fact that it handles invalid xml with more than one root element.**\n\n#### Object Nodes\n\nThere are two kinds of objects. *element* and *text*.\nAn object has always the property ```type```.\nThe other keys depend from this *type*.\n\n##### 'Element' Object Node\n\n```javascript\n{\n  type: [String], // \"element\"\n  tagName: [String], // The tag name of the tag\n  attributes: [Object], // Object containing attributes as properties\n  childNodes: [Array], // Array containing child nodes as object nodes (\"element\" or \"text\")\n  innerXML: [String], // The inner XML of the tag\n  closing: [Boolean], // If the tag is closed typically (\u003c/tagName\u003e)\n  closingChar: [String] || null // If it is not closed typically, the char that is used to close it (\"!\" or \"?\")\n}\n```\n\n##### 'Text' Object Node\n```javascript\n{\n  type: [String], // \"text\"\n  text: [String] // Text contents of the text node\n}\n```\n\n## Stringifier\n\nThe stringifier is the simplest component. Just pass a parsed object structure.\n\n```javascript\nconst xml = require(\"xml-parse\");\n\nvar xmlDoc = [\n  {\n    type: 'element',\n    tagName: '?xml',\n    attributes: {\n      version: '1.0',\n      encoding: 'UTF-8'\n    },\n    childNodes: [],\n    innerXML: '\u003e',\n    closing: false,\n    closingChar: '?'\n  },\n  {\n    type: 'element',\n    tagName: 'root',\n    attributes: {},\n    childNodes: [\n      {\n        type: 'text',\n        text: 'Root Element'\n      }\n    ],\n    innerXML: 'Root Element',\n    closing: true,\n    closingChar: null\n  }\n]\n\nvar xmlStr = xml.stringify(xmlDoc, 2); // 2 spaces\n\nconsole.log(xmlStr);\n```\n\n\n## DOM\n\nThe ```DOM``` method of *xml-parser* instance returns a Document-Object-Model with a few methods.\nIt is oriented on the official W3 DOM but not complex as the original.\n\n```javascript\nconst xml = require(\"xml-parse\");\n\nvar xmlDoc = new xml.DOM(xml.parse('\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e' +\n                                     '\u003croot\u003eRoot Element\u003c/root\u003e')); // Can also be a file path.\n\nxmlDoc.document; // Document Object. (Root)\n```\n\n### 'Element' Object Node\n\n```javascript\n// An element (e.g the 'document' object) has the following prototype methods and properties:\n\nvar objectNode = document.childNodes[1]; // Just an example\n\n// This is the return of a object node element\n\nobjectNode = {\n  type: 'element',\n  tagName: 'tagName',\n  attributes: [Object],\n  childNodes: [Object],\n  innerXML: 'innerXML',\n  closing: true,\n  closingChar: null,\n  getElementsByTagName: [Function], // Returns all child nodes with a specific tagName\n  getElementsByAttribute: [Function], // Returns all child nodes with a specific attribute value\n  removeChild: [Function], // Removes a child node\n  appendChild: [Function], // Appends a child node\n  insertBefore: [Function], // Inserts a child node\n  getElementsByCheckFunction: [Function], // Returns all child nodes that are validated by validation function\n  parentNode: [Circular] // Parent Node\n}\n```\n\n### Handling with child nodes\n\nWith ```appendChild``` or ```insertBefore``` methods of every object node, you are allowed to append a child node. You do not have to do something like ```createElement```.\n\nBecause a child node is just an object literal, with some properties like ```type```, ```tagName```, ```attributes```and more you just have to pass such an object to the function.\n\n#### appendChild\n\n```javascript\nelement.appendChild(childNode); // ChildNode is just a object node\n```\n\n##### Example\n\n```javascript\nconst xml = require('xml-parse');\n\nvar xmlDoc = new xml.DOM(xml.parse('\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e' +\n                                     '\u003croot\u003eRoot Element\u003c/root\u003e'));\n\nvar root = xmlDoc.document.getElementsByTagName(\"root\")[0];\n\nroot.appendChild({\n  type: \"element\",\n  tagName: \"appendedElement\",\n  childNodes: [\n    {\n      type: \"text\",\n      text: \"Hello World :) I'm appended!\"\n    }\n  ]\n});\n```\n\n#### insertBefore\n\n```javascript\nelement.insertBefore(childNode, elementAfter); // ChildNode is just an object literal, 'elementAfter' is just a child node of the parent element\n```\n\n##### Example\n\n```javascript\nconst xml = require('xml-parse');\n\nvar xmlDoc = new xml.DOM(xml.parse('\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e' +\n                                     '\u003croot\u003eRoot Element\u003c/root\u003e'));\n\nvar root = xmlDoc.document.getElementsByTagName(\"root\")[0];\n\nroot.insertBefore({\n  type: \"element\",\n  tagName: \"insertedElement\",\n  childNodes: [\n    {\n      type: \"text\",\n      text: \"Hello World :) I'm appended!\"\n    }\n  ]\n}, root.childNodes[0]);\n```\n\n#### removeChild\n\n```javascript\nelement.removeChild(childNode); // 'childNode' is just a children of the parent element ('element')\n```\n\n##### Example\n\n```javascript\nconst xml = require('xml-parse');\n\nvar xmlDoc = new xml.DOM(xml.parse('\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e' +\n                                     '\u003croot\u003eRoot Element\u003c/root\u003e'));\n\nvar root = xmlDoc.document.getElementsByTagName(\"root\")[0];\n\nroot.removeChild(root.childNodes[0]);\n\n```\n\n### parentNode\n\nThe ```parentNode``` of a object node represents its parent element. It's a ```[Circular]``` reference.\n\n```javascript\nconst xml = require('xml-parse');\n\nvar xmlDoc = new xml.DOM(xml.parse('\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e' +\n                                     '\u003croot\u003eRoot Element\u003c/root\u003e'));\n\nvar root = xmlDoc.document.getElementsByTagName(\"root\")[0];\n\nconsole.log(root.childNodes[0].parentNode); // Returns the 'root' element\n```\n\n## Get child nodes\n\n### getElementsByTagName\n\n```javascript\nelement.getElementsByTagName(\"myTagName\"); // Returns all elements whose tag name is 'myTagName'\n```\n\n### getElementsByAttribute\n\n```javascript\nelement.getElementsByAttribute(\"myAttribute\", \"myAttributeValue\"); // Returns all elements whose attribute 'myAttribute' is 'myAttributeValue'\n```\n\n### getElementsByCheckFunction\n\n```javascript\n// With this method you can set custom 'get' methods.\nelement.getElementsByCheckFunction(function(element) {\n  if (element.type === \"element\" \u0026\u0026 element.childNodes.length == 30) {\n    return true;\n  }\n}); // Returns all elements that have exactly 30 childNodes\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FElyaConrad%2FXML-Parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FElyaConrad%2FXML-Parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FElyaConrad%2FXML-Parser/lists"}