Validate XML, Parse XML, Build XML without C/C++ based libraries
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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.
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 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: falseunpairedTags: []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;
};
}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
});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']
});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;
}