### Install js2xmlparser via npm Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/README.md Use this command to install the package as a dependency in your project. ```bash npm install js2xmlparser ``` -------------------------------- ### XML output example Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/README.md The resulting XML output generated by the provided JavaScript object example. ```xml John Smith Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Summer Time)
3212 22nd St Chicago Illinois 10000
123-555-4567 890-555-1234 567-555-8901 john@smith.com
``` -------------------------------- ### Using Set for Unique Array Items in XML Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Demonstrates how ES2015 Set objects are converted to XML, with each unique item becoming a separate element. Ensure the 'js2xmlparser' package is installed. ```javascript const js2xmlparser = require("js2xmlparser"); const data = { uniqueTags: new Set(["javascript", "nodejs", "xml", "javascript"]) // duplicate removed }; console.log(js2xmlparser.parse("post", data)); ``` -------------------------------- ### Build documentation Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/README.md Generate the project documentation locally using npm. ```bash npm run-script docs ``` -------------------------------- ### Build js2xmlparser from source Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/README.md Commands to clone the repository and build the project using npm scripts. ```bash git clone https://github.com/michaelkourlas/node-js2xmlparser.git npm install npm run-script build ``` -------------------------------- ### Run tests Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/README.md Execute the test suite to verify core functionality. ```bash npm run-script test-prod ``` -------------------------------- ### Convert JavaScript objects to XML Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Demonstrates basic object conversion and handling of nested structures, arrays, and Date objects. ```javascript const js2xmlparser = require("js2xmlparser"); // Basic usage - convert a simple object const person = { firstName: "John", lastName: "Smith" }; console.log(js2xmlparser.parse("person", person)); // Output: // // // John // Smith // // Complex object with nested elements, arrays, and dates const employee = { name: "Jane Doe", department: "Engineering", hireDate: new Date(2020, 5, 15), skills: ["JavaScript", "TypeScript", "Node.js"], contact: { email: "jane@example.com", phone: "555-1234" } }; console.log(js2xmlparser.parse("employee", employee)); // Output: // // // Jane Doe // Engineering // Mon Jun 15 2020 00:00:00 GMT... // JavaScript // TypeScript // Node.js // // jane@example.com // 555-1234 // // ``` -------------------------------- ### Implement mixed content using the # key Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt The # key allows inserting text content directly into elements. Using a Map ensures the order of mixed text and child elements is preserved. ```javascript const js2xmlparser = require("js2xmlparser"); // Simple text value with attributes const link = { "@": { href: "https://example.com" }, "#": "Click here" }; console.log(js2xmlparser.parse("a", link)); // Output: // // Click here // Mixed content with text and child elements using Map for order preservation const paragraph = new Map([ ["#1", "This is "], ["strong", "important"], ["#2", " text with "], ["em", "emphasis"], ["#3", "."] ]); console.log(js2xmlparser.parse("p", paragraph)); // Output: // //

// This is // important // text with // emphasis // . //

``` -------------------------------- ### Formatting Options Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Control XML output formatting with `format` options including pretty-printing, indentation, newlines, and quote style. ```APIDOC ## Formatting Options Control XML output formatting with `format` options including pretty-printing, indentation, newlines, and quote style. ### Request Example ```javascript const js2xmlparser = require("js2xmlparser"); // Custom formatting with tabs and double quotes const options = { format: { pretty: true, indent: "\t", newline: "\r\n", doubleQuotes: true } }; const data = { "@": { id: "123" }, name: "Test" }; console.log(js2xmlparser.parse("root", data, options)); // Output (with tabs and CRLF): // // // Test // // Compact output (no pretty-printing) const compactOptions = { format: { pretty: false } }; console.log(js2xmlparser.parse("root", { a: "1", b: "2" }, compactOptions)); // Output: // 12 ``` ``` -------------------------------- ### Configure XML Declaration Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Control the inclusion, version, encoding, and standalone attributes of the XML declaration. Ensure the `declaration.include` option is set to `true` to enable it. ```javascript const js2xmlparser = require("js2xmlparser"); // Custom declaration with encoding and standalone const options = { declaration: { include: true, version: "1.1", encoding: "UTF-8", standalone: "yes" } }; const data = { message: "Hello World" }; console.log(js2xmlparser.parse("root", data, options)); ``` ```javascript // Disable declaration entirely const optionsNoDecl = { declaration: { include: false } }; console.log(js2xmlparser.parse("root", data, optionsNoDecl)); ``` -------------------------------- ### Parse JavaScript object to XML Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/README.md Demonstrates basic usage of the library to convert a JavaScript object into an XML string. ```javascript var js2xmlparser = require("js2xmlparser"); var obj = { "@": { type: "natural", }, firstName: "John", lastName: "Smith", dateOfBirth: new Date(1964, 7, 26), address: { "@": { type: "home", }, streetAddress: "3212 22nd St", city: "Chicago", state: "Illinois", zip: 10000, }, phone: [ { "@": { type: "home", }, "#": "123-555-4567", }, { "@": { type: "cell", }, "#": "890-555-1234", }, { "@": { type: "work", }, "#": "567-555-8901", }, ], email: "john@smith.com", }; console.log(js2xmlparser.parse("person", obj)); ``` -------------------------------- ### XML Declaration Options Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Configure the XML declaration using the `declaration` option. Control inclusion, XML version, encoding, and standalone attributes. ```APIDOC ## XML Declaration Options Configure the XML declaration using the `declaration` option. Control inclusion, XML version, encoding, and standalone attributes. ### Request Example ```javascript const js2xmlparser = require("js2xmlparser"); // Custom declaration with encoding and standalone const options = { declaration: { include: true, version: "1.1", encoding: "UTF-8", standalone: "yes" } }; const data = { message: "Hello World" }; console.log(js2xmlparser.parse("root", data, options)); // Output: // // // Hello World // // Disable declaration entirely const optionsNoDecl = { declaration: { include: false } }; console.log(js2xmlparser.parse("root", data, optionsNoDecl)); // Output: // // Hello World // ``` ``` -------------------------------- ### Configure XML Formatting Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Customize the output format of the XML, including enabling pretty-printing, setting indentation characters, defining newline characters, and choosing between single or double quotes for attributes. ```javascript const js2xmlparser = require("js2xmlparser"); // Custom formatting with tabs and double quotes const options = { format: { pretty: true, indent: "\t", newline: "\r\n", doubleQuotes: true } }; const data = { "@": { id: "123" }, name: "Test" }; console.log(js2xmlparser.parse("root", data, options)); ``` ```javascript // Compact output (no pretty-printing) const compactOptions = { format: { pretty: false } }; console.log(js2xmlparser.parse("root", { a: "1", b: "2" }, compactOptions)); ``` -------------------------------- ### Using Map for Ordered Elements Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt JavaScript objects don't guarantee property order. Use ES2015 `Map` when element order matters. Map keys are converted to strings for element names. ```APIDOC ## Using Map for Ordered Elements JavaScript objects don't guarantee property order. Use ES2015 `Map` when element order matters. Map keys are converted to strings for element names. ### Request Example ```javascript const js2xmlparser = require("js2xmlparser"); // Use Map to guarantee element order const orderedData = new Map([ ["first", "1st item"], ["second", "2nd item"], ["third", "3rd item"], ["@", { ordered: "true" }] // Attributes still work in Maps ]); console.log(js2xmlparser.parse("items", orderedData)); // Output (order guaranteed): // // // 1st item // 2nd item // 3rd item // // Complex ordered structure const form = new Map([ ["@", { action: "/submit", method: "POST" }], ["input", { "@": { type: "text", name: "username" } }], ["input", { "@": { type: "password", name: "password" } }], ["button", { "@": { type: "submit" }, "#": "Login" }] ]); ``` ``` -------------------------------- ### parse(root, object, options?) Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt The primary function to convert a JavaScript object into an XML string representation. ```APIDOC ## parse(root, object, options?) ### Description Converts a JavaScript object into an XML string representation. It supports JSON-type objects, arrays, primitive data types, and native objects like Date and RegExp. ### Parameters - **root** (string) - Required - The name of the root XML element. - **object** (object) - Required - The JavaScript object to convert. - **options** (object) - Optional - Configuration object for formatting, indentation, and XML declarations. ### Response - **string** - A complete XML document string. ``` -------------------------------- ### Add Document Type Definition (DTD) Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Include a DTD in the XML document by setting `dtd.include` to `true`. Specify the DTD name and optionally provide public and system identifiers. ```javascript const js2xmlparser = require("js2xmlparser"); const options = { dtd: { include: true, name: "html", pubId: "-//W3C//DTD XHTML 1.0 Strict//EN", sysId: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" } }; const page = { head: { title: "My Page" }, body: { p: "Hello World" } }; console.log(js2xmlparser.parse("html", page, options)); ``` -------------------------------- ### Define XML attributes using the @ key Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Use the @ key to map object properties to XML attributes. Multiple attribute blocks can be defined using numeric suffixes. ```javascript const js2xmlparser = require("js2xmlparser"); const person = { "@": { type: "natural", id: "12345" }, firstName: "John", lastName: "Smith", address: { "@": { type: "home" }, streetAddress: "3212 22nd St", city: "Chicago", state: "Illinois", zip: 10000 }, phone: [ { "@": { type: "home" }, "#": "123-555-4567" }, { "@": { type: "cell" }, "#": "890-555-1234" } ] }; console.log(js2xmlparser.parse("person", person)); // Output: // // // John // Smith //
// 3212 22nd St // Chicago // Illinois // 10000 //
// 123-555-4567 // 890-555-1234 //
``` -------------------------------- ### Array Wrapping with Wrap Handlers Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Control how array elements are wrapped in XML using the `wrapHandlers` option. Each handler specifies the inner element name for a given array key. ```javascript const js2xmlparser = require("js2xmlparser"); const options = { wrapHandlers: { // Wrap "items" array with individual "item" elements "items": (key, value) => "item", // Wrap "tags" array with individual "tag" elements "tags": () => "tag" } }; const data = { name: "Shopping List", items: ["Apples", "Bread", "Milk"], tags: ["groceries", "weekly"] }; console.log(js2xmlparser.parse("list", data, options)); ``` -------------------------------- ### Custom Type Conversion with Type Handlers Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Define custom conversion functions for JavaScript types using the `typeHandlers` option. This allows for specific formatting or suppression of values. Use `Absent.instance` to omit values from the output. ```javascript const js2xmlparser = require("js2xmlparser"); const { Absent } = require("js2xmlparser"); const options = { typeHandlers: { // Format Date objects as ISO strings "[object Date]": (value) => value.toISOString().split('T')[0], // Suppress null values from output "[object Null]": () => Absent.instance, // Format numbers with 2 decimal places "[object Number]": (value) => value.toFixed(2) } }; const data = { product: "Widget", price: 19.99, quantity: 5, createdAt: new Date(2023, 5, 15), deletedAt: null, discount: 0.15 }; console.log(js2xmlparser.parse("order", data, options)); ``` -------------------------------- ### XML Attributes and Mixed Content Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Special keys used to define XML attributes and mixed content within the object structure. ```APIDOC ## XML Attributes and Mixed Content ### Description Use special keys to control XML-specific output structures. ### Attributes - **@** (object) - Used to define XML attributes for the parent element. Maps attribute names to values. ### Mixed Content - **#** (string) - Used to insert text content directly within an element, enabling mixed content scenarios. ``` -------------------------------- ### Dynamic Element Naming with '=' Key Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Use the '=' key within your data object to dynamically set the XML element name for that object. This is useful when the element name depends on the data itself. ```javascript const js2xmlparser = require("js2xmlparser"); const data = { items: [ { "=": "book", title: "JavaScript Guide" }, { "=": "magazine", title: "Tech Weekly" }, { "=": "newspaper", title: "Daily News" } ] }; console.log(js2xmlparser.parse("library", data)); ``` -------------------------------- ### Parse to Existing XML Element Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Append XML content to an existing `xmlcreate.XmlElement` object without creating a new document. This method ignores declaration and DTD options. ```javascript const js2xmlparser = require("js2xmlparser"); const { XmlDocument } = require("xmlcreate"); // Create a document manually with xmlcreate const doc = new XmlDocument({ validation: true }); doc.decl({ version: "1.0", encoding: "UTF-8" }); // Create root element const root = doc.element({ name: "catalog" }); // Add first product using parseToExistingElement const product1 = root.element({ name: "product" }); js2xmlparser.parseToExistingElement(product1, { "@": { id: "001" }, name: "Widget", price: 9.99 }); // Add second product const product2 = root.element({ name: "product" }); js2xmlparser.parseToExistingElement(product2, { "@": { id: "002" }, name: "Gadget", price: 19.99 }); console.log(doc.toString({ pretty: true })); ``` -------------------------------- ### Document Type Definition (DTD) Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Add a DTD to the XML document using the `dtd` option. Specify the DTD name, and optionally system and public identifiers. ```APIDOC ## Document Type Definition (DTD) Add a DTD to the XML document using the `dtd` option. Specify the DTD name, and optionally system and public identifiers. ### Request Example ```javascript const js2xmlparser = require("js2xmlparser"); const options = { dtd: { include: true, name: "html", pubId: "-//W3C//DTD XHTML 1.0 Strict//EN", sysId: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" } }; const page = { head: { title: "My Page" }, body: { p: "Hello World" } }; console.log(js2xmlparser.parse("html", page, options)); // Output: // // // // // My Page // // //

Hello World

// // ``` ``` -------------------------------- ### Use Map for Ordered XML Elements Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Utilize ES2015 `Map` objects to ensure the order of elements in the generated XML, as standard JavaScript objects do not guarantee property order. Map keys are converted to strings for element names. ```javascript const js2xmlparser = require("js2xmlparser"); // Use Map to guarantee element order const orderedData = new Map([ ["first", "1st item"], ["second", "2nd item"], ["third", "3rd item"], ["@", { ordered: "true" }] // Attributes still work in Maps ]); console.log(js2xmlparser.parse("items", orderedData)); ``` ```javascript // Complex ordered structure const form = new Map([ ["@", { action: "/submit", method: "POST" }], ["input", { "@": { type: "text", name: "username" } }], ["input", { "@": { type: "password", name: "password" } }], ["button", { "@": { type: "submit" }, "#": "Login" }] ]); ``` -------------------------------- ### parseToExistingElement Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Adds XML content to an existing xmlcreate XmlElement object without creating a new document. This is useful for building XML documents incrementally or integrating with existing xmlcreate workflows. Declaration and DTD options are ignored. ```APIDOC ## parseToExistingElement(element, object, options?) Adds XML content to an existing xmlcreate XmlElement object without creating a new document. This is useful for building XML documents incrementally or integrating with existing xmlcreate workflows. Declaration and DTD options are ignored. ### Request Example ```javascript const js2xmlparser = require("js2xmlparser"); const { XmlDocument } = require("xmlcreate"); // Create a document manually with xmlcreate const doc = new XmlDocument({ validation: true }); doc.decl({ version: "1.0", encoding: "UTF-8" }); // Create root element const root = doc.element({ name: "catalog" }); // Add first product using parseToExistingElement const product1 = root.element({ name: "product" }); js2xmlparser.parseToExistingElement(product1, { "@": { id: "001" }, name: "Widget", price: 9.99 }); // Add second product const product2 = root.element({ name: "product" }); js2xmlparser.parseToExistingElement(product2, { "@": { id: "002" }, name: "Gadget", price: 19.99 }); console.log(doc.toString({ pretty: true })); // Output: // // // // Widget // 9.99 // // // Gadget // 19.99 // // ``` ``` -------------------------------- ### CDATA Section Handling Source: https://context7.com/michaelkourlas/node-js2xmlparser/llms.txt Manage CDATA sections for content that may contain invalid XML characters or for specific keys. `cdataInvalidChars` automatically wraps problematic content, while `cdataKeys` allows explicit specification. ```javascript const js2xmlparser = require("js2xmlparser"); // Automatic CDATA for invalid XML characters const options1 = { cdataInvalidChars: true }; const script = { code: "if (a < b && c > d) { return true; }", description: "Simple comparison" }; console.log(js2xmlparser.parse("script", script, options1)); ``` ```javascript // Explicit CDATA keys const options2 = { cdataKeys: ["content", "raw"] }; const article = { title: "XML Tutorial", content: "Use for tags", raw: "Special & characters" }; console.log(js2xmlparser.parse("article", article, options2)); ``` -------------------------------- ### Parse JavaScript object to XML Source: https://github.com/michaelkourlas/node-js2xmlparser/blob/master/CHANGES.md Use the parse method to convert a JavaScript object into an XML string. This method replaces the deprecated direct function call. ```javascript var js2xmlparser = require("js2xmlparser"); var root = "root"; var data = {hello: "world"}; var options = {}; // old method (no longer works): // js2xmlparser(root, data, options); // new method: js2xmlparser.parse(root, data, options); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.