XML Library
This Library allows you to convert XML strings into the JSON Format and the other way around.
Installing this Library
Run the following command
$ npm install xml-library --save
Then load it inside your node.js Application
const XML = ;
XML (Extensible Markup Language)
Parsing XML
To interpret XML Strings as JSON Objects, you can do
XML;
For help on how to use the 'json' Object, see XMLNode
Parsing JSON
You can also interpret JSON objects back into XML.
BUT: This JSON object must be an XMLNode Object (more Info down below)
'options' (if specified) is a simple JSON Object with keys and values. (Missing keys will be inserted with a default value)
json;
Key | Description | Default Value |
---|---|---|
indent | Number of spaces prepended on each new level | 2 |
new_lines | Whether new lines shall be used. Set it to false if you want the entire XML string to be on a single line | true |
header | A header that is prepended to the entire xml string (e.g. when you are messing with HTML documents or other important document settings) | <?xml version="1.0" encoding="UTF-8"?> |
json;
XMLNode
This module adds a class called "XMLNode".
The JSON Object returned when Parsing XML is an instance of this class.
Parsing JSON Objects also requires an instance of this class.
You can use the class in your node.js Application using
const XML = ;const XMLNode = XMLXMLNode;
Or by direct deconstruction of the module.
const XMLNode = ;
Constructor
The constructor requires you to specify a Name for the element.
name;"project";
You can also specify attributes.
name attributes;"project" "version": "2.3.1" "author": "TheBusyBiscuit";
Or you can specify a value.
name value;"project" "XML-Library";
XML-Library
Or a value and attributes.
name attributes value;"project" "version": "2.3.1" "author": "TheBusyBiscuit" "XML-Library";
XML-Library
Methods
For the following examples, we work with this node as our root.
var node "project" "version": "2.3.1" "author": "TheBusyBiscuit";
.addChild(node)
The specified child, must be an instance of XMLNode of course.
But it can also be an array of XMLNode instances.
node;
JavaScript
.setChild(key, node)
The specified child, must be an instance of XMLNode of course.
Because XML elements can have multiple children with the same name, each child needs to have an index. (e.g. language[0])
This index will be omitted in the actual XML String when Parsing JSON Objects
If no index is specified, "[0]" will be appended to the name.
node;node;node;
Java C#
.setAttribute(key, value)
Pretty self-explaining.
Specify a key (String) and a value (String) and set this as an attribute.
node;
If 'value' is null and an attribute with that name exists, then the attribute is removed.
node;
.setValue(value)
Pretty self-explaining.
Specify a value (String) and you set the inner content of your node.
Specify no value and you will remove the inner content of your node (This does not remove any children)
node;
XML-Library
.getChild(path)
Specify the name of a child to get its instance.
Java C#
The name can include an index, if there are multiple children sharing the same name.
node;
C#
If no index is specified, it is going to return the first child with that name.
node;
Java
But you can also specify an array of names, to get the node's grandchildren or great grandchildren or ... (You get the idea.)
node;
Java
Here, you can specify an index to target a certain child at a certain point in the tree again.
.asXMLString(options, callback)
Copyright / Licensing
Copyright (c) 2018 TheBusyBiscuit
Licensed under the MIT License.