React XML Viewer

3.0.4 · active · verified Tue Apr 21

react-xml-viewer is a React component designed for pretty-printing and displaying XML strings in a user-friendly, formatted manner. The current stable version is 3.0.4. The library has an active release cadence, with recent updates addressing security vulnerabilities (CVEs), ensuring compatibility with newer React versions (e.g., React 19), and adding new features like line numbers and improved collapsing functionality. Its key differentiators include extensive customization options for appearance via a theming prop, the ability to make XML collapsible, and a dedicated prop for handling invalid XML input. It also ships with TypeScript types, enhancing developer experience in TypeScript projects. It leverages `fast-xml-parser` for its underlying XML processing, and regular updates help maintain security and performance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates rendering a complex XML string using `XMLViewer` with various styling, collapsing, and line number options, including custom theme and invalid XML handling.

import React from 'react';
import XMLViewer from 'react-xml-viewer';

const exampleXml = `
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J.K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>
`;

const customInvalidXmlMessage = (
  <div style={{ color: 'red', fontWeight: 'bold' }}>
    Oops! This XML is malformed.
  </div>
);

export function App() {
  return (
    <div style={{ fontFamily: 'monospace', padding: '20px' }}>
      <h1>My XML Document</h1>
      <XMLViewer
        xml={exampleXml}
        indentSize={4}
        collapsible={true}
        initialCollapsedDepth={1}
        showLineNumbers={true}
        invalidXml={customInvalidXmlMessage}
        theme={{
          tagColor: '#61dafb',
          textColor: '#212121',
          attributeKeyColor: '#e91e63',
          attributeValueColor: '#4caf50'
        }}
      />
    </div>
  );
}

view raw JSON →