NOTE: the master branch of the source repo for this project did not compile. It also did not support xpath queries. This fork updates the code so that master compiles, augments the testing and adds xpath support.
It fully supports namespaces, derived types and substitution groups.
It can handle pretty hairy schema such as
WFS and extensions to them defined by
Output is fully typed and structured according to the actual meaning of input data, as defined in the schema.
For example this XML:
can become this JSON (run
npm test to see it happen):
Note the following:
"123"can be a string or a number depending on the context.
ownerchild element are represented in the same way.
dirhas a single owner but can contain many files, so
fileis an array but
- Output data types are as simple as possible while correctly representing the input.
There's much more. What if we parse an empty dir:
Now we can print the result and try some magical features:
Unseen in the JSON output, every object is an instance of a constructor for the appropriate XSD schema type.
Its prototype also contains placeholders for valid children, which means you can refer to
a.b.c.d._exists even if
a.b doesn't exist.
This saves irrelevant checks when only the existence of a deeply nested item is interesting.
_exists flag is
true in the prototypes and
false in the placeholder instances, so it consumes no memory per object.
We can also process data as soon as the parser sees it in the incoming stream:
The best part: your code is fully typed with comments pulled from the schema! See the screenshot at the top.
- node-xml4js uses schema information to read XML into nicely structured objects.
Copyright (c) 2016-2017 BusFaster Ltd