XML 1.0 parsing, stringifying, validating, converting to and from JSON
XMJS is an XML parsing library for JavaScript. It has a parse and a stringify method, similar to the built-in JSON library and a ton of more features!
js
const XML = require("xmjs");
`
$3
For parsing XML, you can run the following:
`js
XML.parse("hello world ");
/*
{
ExampleXML: {
value: 'hello world',
attributes: {}
}
}
*/
`
This is how XMJS parses nested tags:
`js
XML.parse("I am a nested tag! ");
/*
{
SupportsNesting: {
NestedTag: {
value: 'I am a nested tag!',
attributes: {}
}
}
}
*/
`
XMJS also supports parsing attributes of tags:
`js
XML.parse(" ");
/*
{
SomeTag: {
value: '',
attributes: {
hello: 'world'
}
}
}
*/XML.parse(" ");
/*
{
Person: {
Job: {
value:'',
attributes: {
income: '500$'
}
},
attributes: {}
}
}
*/
`
XMJS supports XML arrays since version 1.1.0:
`js
XML.parse();/*
{
Person: {
FavoriteFoods: {
Food: [
{
value: "Cheeseburger",
attributes: {}
},
{
value: "Pizza",
attributes: {}
},
{
value: "Broccoli",
attributes: {}
},
{
value: "Doner",
attributes: {}
}
]
},
attributes: {
name: "John"
}
}
}
*/
`
$3
XMJS can stringify JavaScript objects, just like how the JSON.stringify method does it
`js
XML.stringify({
Person: {
Name: "Jonathan",
Surname: "Doe",
EMail: "john@doe.com",
Job: {
Income: "500$"
}
}
});
/*
Jonathan
Doe
john@doe.com
500$
*/
`
$3
XMJS can validate XML through XML.validate:
`js
XML.validate("hello world "); // Parses and returns { Tag: { value: 'hello world', attributes: {} } }XML.validate("i am not valid XML"); // Returns false
`$3
XMJS can convert XML data to JSON:
`js
XML.xmlToJson("Engineer ");
/*
{
"Person": {
"Job": {
"value": "Engineer",
"attributes": {
"income": "500$"
}
},
"attributes": {
"name": "Jonathan",
"surname":"Doe"
}
}
}
*/
`$3
XMJS can also convert JSON data to XML:
`js
XML.jsonToXml("{ \"Hello\": \"World\" }");
/*
World
*/
`Changelog
1.0.0
* Initial Release
1.0.1
* Minor bug fixes
1.0.2
* Minor bug fixes
1.0.3
* Minor bug fixes
1.1.0
* Added support for XML arrays
* Added option disallowUnexpectedTokenError: allows unexpected tokens to exist in an XML document without throwing an error.
* Added changelog to README.md
1.1.1
* Fixed XML arrays not parsing correctly
1.1.2
* Fixed last attribute overridng all other attributes in XML objects ( would actually only have attribute c)
1.1.3
* Added ability to use -` in XML keys