CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-fast-xml-parser

Validate XML, Parse XML, Build XML without C/C++ based libraries

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

validation.mddocs/

XML Validation

Fast validation of XML syntax and structure without the overhead of full parsing. The validation system checks for well-formed XML, proper tag nesting, attribute syntax, and other structural requirements.

Capabilities

XMLValidator.validate

Validates XML syntax and returns detailed error information including line and column numbers for any issues found.

/**
 * Validates XML syntax and structure
 * @param xmlData - XML content as string
 * @param options - Optional validation configuration
 * @returns true if valid, ValidationError object if invalid
 */
XMLValidator.validate(xmlData: string, options?: validationOptions): true | ValidationError;

Usage Examples:

import { XMLValidator } from "fast-xml-parser";

// Basic validation
const xmlString = "<root><item>value</item></root>";
const result = XMLValidator.validate(xmlString);

if (result === true) {
  console.log("XML is valid");
} else {
  console.log(`Invalid XML at line ${result.err.line}, column ${result.err.col}: ${result.err.msg}`);
}

// Validation with options
const optionsResult = XMLValidator.validate(xmlString, {
  allowBooleanAttributes: true,
  unpairedTags: ['br', 'hr', 'img']
});

Configuration Types

validationOptions

Configuration options for XML validation behavior.

interface validationOptions {
  /** Allow attributes without values (e.g., <input checked>) */
  allowBooleanAttributes?: boolean;
  
  /** List of tag names that don't require closing tags */
  unpairedTags?: string[];
}

Default values:

  • allowBooleanAttributes: false
  • unpairedTags: []

ValidationError

Error object returned when validation fails, containing detailed error information.

interface ValidationError {
  err: {
    /** Error code identifier */
    code: string;
    
    /** Human readable error message */
    msg: string;
    
    /** Line number where error occurred (1-based) */
    line: number;
    
    /** Column number where error occurred (1-based) */
    col: number;
  };
}

Common Validation Scenarios

Boolean Attributes

HTML-style attributes without values:

import { XMLValidator } from "fast-xml-parser";

// This will fail with default options
const htmlLike = '<input type="text" required>';

// Enable boolean attributes
const result = XMLValidator.validate(htmlLike, {
  allowBooleanAttributes: true
});

Self-Closing Tags

Tags that don't need closing tags (common in HTML):

import { XMLValidator } from "fast-xml-parser";

// This will fail with default options
const htmlContent = '<p>Hello<br>World</p>';

// Allow br as unpaired tag
const result = XMLValidator.validate(htmlContent, {
  unpairedTags: ['br', 'hr', 'img', 'input']
});

Error Handling

import { XMLValidator } from "fast-xml-parser";

function validateAndHandle(xmlString) {
  const result = XMLValidator.validate(xmlString);
  
  if (result !== true) {
    // Extract error details
    const { code, msg, line, col } = result.err;
    
    throw new Error(`XML Validation Error (${code}) at ${line}:${col} - ${msg}`);
  }
  
  return true;
}

Performance Notes

  • Validation is much faster than full parsing
  • Suitable for pre-processing large XML files
  • Memory efficient - doesn't build object trees
  • Stops at first error for fast failure detection

docs

building.md

index.md

parsing.md

validation.md

tile.json