https://github.com/nanonid/xmljsonkit
XML to JSON parser library with ordered elements based on Chevrotain
https://github.com/nanonid/xmljsonkit
json parser xml-parse
Last synced: about 1 month ago
JSON representation
XML to JSON parser library with ordered elements based on Chevrotain
- Host: GitHub
- URL: https://github.com/nanonid/xmljsonkit
- Owner: Nanonid
- License: apache-2.0
- Created: 2020-08-27T14:24:53.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-31T18:27:28.000Z (almost 6 years ago)
- Last Synced: 2025-10-13T05:14:24.804Z (9 months ago)
- Topics: json, parser, xml-parse
- Language: TypeScript
- Homepage:
- Size: 130 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
xmljsonkit
==
XML to JSON parser library with ordered elements.
**xmljsonkit** is a XML to JSON parser library that is built on the fast and extensible [Chevrotain](https://sap.github.io/chevrotain/docs/) parser.
**xmljsonkit** is a small extenion of the [Chevrotain Example XML Parser](https://github.com/SAP/chevrotain/tree/master/examples/grammars/xml) with a JSON Concrete Syntax Tree (CST) visitor that can be extended for special XML syntax or JSON handling.
Values that are simple strings will be converted to numbers, if possible.
The conversion process is configurable.
Simple string text values will be promoted from objects to string property values for more convinient JSON handling.
```typescript
"Computer" => { genre:"Computer" }
" 44.95 " => { price: 44.95 }
```
There are two parsers:
1. xml2json which does simple JSON object translation.
2. orderedxml2json which extends xml2json for maintaining order of elements.
The XML Lexer, Parser, and JSON Visitor are all easily extended or changed to meet your exact JSON translation requirements.
Ordered XML Parsing
---
Sometimes order of elements is critical, for example with the XML represents a graphical format with serialized operations that must be executed in order.
For example, these shapes or stencils have path commands which must be executed in order. Other **xml2json** libraries do *NOT* maintain order, but **xmljsonkit** orderedxml2json does maintain order.
orderedxml2json takes a list of property names that require ordered array handling, and only builds the **__elements** ordered array for that elements contents.
```xml
```
Creates JSON with the correct order in a reserved array property called **__elements**.
```typescript
const ORDERD_PROPERTY = "__elements";
var shapes = result.shapes.shape as any[];
var shape = shapes[0];
expect(shape._name).toBe("or");
expect(shape.background).toBeDefined();
expect(shape.background.path).toBeDefined();
expect(shape.background.path.__elements).toBeDefined();
var path = shape.background.path.__elements as any[];
expect(path.length).toBe(4);
expect(path[0]).toEqual({move: { _x:0, _y:0}});
expect(path[1]).toEqual({quad: { _x1:100, _y1:0, _x2:100, _y2:50}});
expect(path[2]).toEqual({quad: { _x1:100, _y1:100, _x2:0, _y2:100}});
expect(path[3]).toEqual({close: {}});
```
Emedded text between elements is also preserved.
```xml
Before authorGambardella, Matthew
XML Developer's Guide
Computer
44.95 after price2000-10-01
An in-depth look at creating applications
with XML.Last
```
Maintains the correct order of text and XML elements.
```typescript
const ORDERD_PROPERTY = "__elements";
const books = result.catalog.book;
expect(books.length).toBe(2);
var book = books[0];
expect(book._id).toBe("bk101");
expect(book.__elements).toBeDefined();
var bookProps:any[] = book.__elements;
expect(bookProps.length).toBe(9);
expect(bookProps[0]["#text"]).toBe("Before author");
expect(bookProps[1].author).toBe("Gambardella, Matthew");
expect(bookProps[4].price).toBe(44.95);
expect(bookProps[5]["#text"]).toBe("after price");
expect(bookProps[6].publish_date).toBe("2000-10-01");
expect(bookProps[8]["#text"]).toBe("Last");
```