Fault-tolerant parsing
Reads XML and builds a usable document tree, even when the file is incomplete or has syntax errors.
This tool helps you work with XML in JavaScript and TypeScript projects. It can understand XML even when it has errors, and it gives editor-like features without needing Java or native software.
npm install xml-language-service
Install the package in an ESM-compatible Node.js project.
npm install xml-language-service
Create a service, parse XML, then call the feature you need.
import { getLanguageService } from "xml-language-service";
const service = getLanguageService();
const xml = `<catalog>
<book id="bk101">
<title>XML Developer Guide</title>
</book>
</catalog>`;
const document = service.parseXMLDocument("file:///catalog.xml", xml);
const completions = service.doComplete(document, { line: 1, character: 4 });
const hover = service.doHover(document, { line: 2, character: 6 });
const symbols = service.findDocumentSymbols(document);
const formatting = service.format(document, { tabSize: 2, insertSpaces: true });
console.log(completions.items);
console.log(hover);
console.log(symbols);
console.log(formatting);
service.dispose();
The service returns plain TypeScript objects, so you can connect these features to your own editor, CLI, web app, language server, or test tool.
Reads XML and builds a usable document tree, even when the file is incomplete or has syntax errors.
Suggests element names, attributes, and closing tags while the user is typing.
Shows useful information for the XML element, attribute, or comment under the cursor.
Creates an outline of the XML structure so editors can show breadcrumbs, sidebars, and quick navigation.
Finds XML blocks that can be collapsed, such as large nested elements.
Returns text edits that format XML with clean indentation and spacing.
Renames matching opening and closing tags together so XML stays consistent.
Jumps between matching tags and finds other elements with the same tag name.
Checks XML against XSD schemas using Apache Xerces-C++ compiled to WebAssembly.
Register an XSD schema once, then validate parsed XML documents against it.
await service.registerSchema({
uri: "file:///schema.xsd",
xsdText: schemaText,
});
const document = service.parseXMLDocument("file:///catalog.xml", xmlText);
const diagnostics = await service.validate("file:///schema.xsd", document);
Schema bundles are also supported for XSD files that use
xs:include or xs:import.
Use the printer helpers when you want to see what the parser produced.
console.log(service.printTreeAST(document));
console.log(service.printAST(document));
console.log(service.printCST(document));
| Resource | URL |
|---|---|
| npm package | npmjs.com/package/xml-language-service |
| Repository | github.com/harshanacz/xml-language-service |
| Project using this | github.com/harshanacz/xml-review-bot-demo |
| XML language server based on this | github.com/harshanacz/xml-language-server |