Giter VIP home page Giter VIP logo

tsxml's Introduction

tsxml

A no-dependency XML parser with DOM-like AST and XML serialization for node.js and the browser.

Overview

tsxml is a small, not (yet) spec compliant XML library that contains a liberal parser, a syntax tree similar to, but not as complicated as, the DOM. The syntax tree can be used to build XML manually. It can serialize formatted and compact XML.

It supports all kinds of XML nodes, including comments, markup declaration openers (such as <!DOCTYPE ...>), processing instructions (<?svg...) and self closing (<foo />) and "void" (<foo>) nodes, meaning it's capable of parsing HTML and SVG, too.

Is it used somewhere?

Yup, here: vs-code-xml-format.

Status Quo

Parser

The parser is:

  • liberal in what it accepts

The parser is not (yet):

  • incremental
  • capable of streaming

At the moment the parser only processes complete XML strings at once, incremental parsing is not possible yet (which can be a performance issue with huge documents). If you need incremental parsing and are comfortable with using streams, check out sax-js.

Schema Validation

Does not exist. At least not yet.

Syntax Tree

The syntax tree can be built manually (see examples) or by the parser. It is similar to the DOM, but not quite as complex. It can be used to traverse (in fact, the parser uses AST nodes it creates to traverse while parsing) and to serialize XML.

To-Do-List:

  • XPath support

XML Serialization (converting objects to XML strings)

Syntax tree nodes support XML serialization with their toString() and toFormattedString() methods, for example:

const document = xml.Compiler.parseStringToAst(`
	<foo><bar /></foo>
`);
/*
	The following 2 statements will log '<foo><bar /></foo>' 
*/
console.log(node.toString());
console.log(node + '');
/*
	The following statement will log:
		<foo>
			<bar />
		</foo> 
*/
node.toFormattedString();

Formatting can also be done in a somewhat more concise way:

console.log(await xml.Compiler.formatXmlString('<foo><bar /></foo>'));
/*
	...which will log:
		<foo>
			<bar />
		</foo> 
*/

Why build another XML parser, aren't there plenty already?

I see your point. There's plenty of good parsers around and most of them get the job done alright. What I wanted, however, is a tool that runs in every environment (browser, node.js, you name it), doesn't come with a myriad of dependencies (especially ones that run in node's C-space), that doesn't just ignore comments, MDOs and processing instructions (which are a huge no-no in some other libraries) and that, ideally, has a syntax tree that allows traversal similar to the DOM instead of just providing plain old objects. Oh, right, there should be a way to serialize XML as well.

If you don't need all of this, you might want to check out these:

Alternatives

License

MIT. See LICENSE file.

tsxml's People

Watchers

Ian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.