Registry / serialization / react-xml-parser

react-xml-parser

JSON →
library1.1.8jsnpmunverified

react-xml-parser is a lightweight XML parsing library specifically designed for React Native environments. It provides basic functionality to convert XML string data into a traversable JavaScript object structure and can also serialize an XML object back into a string. Currently at version 1.1.8, the package has not seen updates since late 2020, indicating it is in a maintenance-only state. Its primary differentiation lies in its simplicity and explicit focus on React Native, offering a pure JavaScript solution without native module dependencies. This makes it suitable for parsing simple, well-formed XML payloads, though its lack of active development means it may not support newer XML features or offer optimized performance for very large or complex documents compared to more actively developed alternatives.

npm install react-xml-parser
INSTALL
IMPORT
SIG · REACT-XML-PARSER
R
react-xml-parser
serializationjavascriptv1.1.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

XMLParser
import XMLParser from 'react-xml-parser';
import { XMLParser } from 'react-xml-parser';
The package exports a default class. While originally designed for CommonJS `require`, modern React Native environments with Babel can transpile the ESM default import correctly. Avoid named imports as they will likely fail.
XMLParser (CommonJS)
const XMLParser = require('react-xml-parser');
import XMLParser from 'react-xml-parser';
This is the original CommonJS import style demonstrated in the package's documentation. It is still valid in Node.js environments or React Native setups that support CommonJS.

This React Native component demonstrates how to use `react-xml-parser` to parse a static XML string. It initializes the parser, converts the XML string into a JavaScript object, and then displays specific elements from the parsed structure within the UI. It also includes basic error handling for parsing failures.

import React, { useState, useEffect } from 'react'; import { View, Text, ScrollView, StyleSheet } from 'react-native'; import XMLParser from 'react-xml-parser'; const sampleXml = `<?xml version='1.0' encoding='utf-8'?> <Library> <Books count='1'> <Book id='1'> <Name>Me Before You</Name> <Author>Jojo Moyes</Author> </Book> </Books> <Music count='1'> <CD id='2'> <Name>Houses of the Holy</Name> <Artist>Led Zeppelin</Artist> </CD> </Music> </Library>`; const XMLDisplayComponent = () => { const [parsedData, setParsedData] = useState(null); const [error, setError] = useState(null); useEffect(() => { try { const parser = new XMLParser(); const xmlDoc = parser.parseFromString(sampleXml); setParsedData(xmlDoc); } catch (e) { setError('Error parsing XML: ' + e.message); console.error(e); } }, []); return ( <ScrollView style={styles.container}> <Text style={styles.header}>Parsed XML Data:</Text> {error ? ( <Text style={styles.errorText}>{error}</Text> ) : parsedData ? ( <View> <Text style={styles.sectionHeader}>Library Name: {parsedData.name}</Text> <Text style={styles.text}>Books Count: {parsedData.children[0].attributes.count}</Text> <Text style={styles.text}>Book Name: {parsedData.getElementsByTagName('Name')[0]?.value}</Text> <Text style={styles.text}>Book Author: {parsedData.getElementsByTagName('Author')[0]?.value}</Text> <Text style={styles.text}>CD Name: {parsedData.getElementsByTagName('Name')[1]?.value}</Text> <Text style={styles.text}>CD Artist: {parsedData.getElementsByTagName('Artist')[0]?.value}</Text> </View> ) : ( <Text style={styles.text}>Loading or parsing XML...</Text> )} </ScrollView> ); }; const styles = StyleSheet.create({ container: { flex: 1, padding: 20, backgroundColor: '#fff', }, header: { fontSize: 22, fontWeight: 'bold', marginBottom: 15, }, sectionHeader: { fontSize: 18, fontWeight: '600', marginTop: 10, marginBottom: 5, }, text: { fontSize: 16, marginBottom: 5, }, errorText: { fontSize: 16, color: 'red', }, }); export default XMLDisplayComponent;
Debug
Known issues
gotchaThe `react-xml-parser` package has not been updated since October 2020. This means it may lack support for modern XML features, security patches, or compatibility with newer versions of React Native or Node.js environments.
fix
For new projects or complex XML parsing needs, consider more actively maintained alternatives like `fast-xml-parser`, `xml2js`, or `react-native-turboxml` (for native performance in React Native) which offer better performance, broader feature sets, and ongoing support.
affects: <=1.1.8
gotchaThis parser is designed for 'simple XML responses' and may not correctly handle advanced XML features such as namespaces, DTDs, CDATA sections as separate nodes, or complex XPath queries. Its internal object structure is straightforward but might not map directly to all XML complexities.
fix
For XML with namespaces or other complex structures, evaluate the output object carefully. If standard DOM API behavior or advanced querying is required, consider `xmldom` (for DOM-like API) or `fast-xml-parser` with specific configuration options for handling namespaces, attributes, and text nodes.
affects: <=1.1.8
gotchaBeing a pure JavaScript parser, `react-xml-parser` runs on the JavaScript thread. Parsing very large XML files or performing many parsing operations sequentially might lead to performance bottlenecks and UI freezes in React Native applications.
fix
For performance-critical applications dealing with large XML payloads, explore native XML parsers for React Native, such as `react-native-turboxml`, which offload parsing to background threads. Alternatively, consider pre-processing XML on a backend or converting it to a more efficient format like JSON before sending it to the client.
affects: <=1.1.8
Errors
Common errors & fixes
TypeError: XMLParser is not a constructor
This error typically occurs when trying to instantiate `XMLParser` using a named import or incorrect default import syntax, particularly in environments with specific CommonJS/ESM interop settings.
fix
Ensure you are importing the `XMLParser` class as a default import: `import XMLParser from 'react-xml-parser';` for ESM contexts, or `const XMLParser = require('react-xml-parser');` for CommonJS environments. The package exports a class directly as its default.
XML parsing error: 'Invalid XML document'
The `parseFromString` method throws an error if the input XML string is malformed, not well-formed (e.g., unmatched tags, invalid characters), or contains syntax errors.
fix
Verify the integrity and correctness of your XML string. Use an XML validator tool to check for syntax errors. Ensure that all tags are properly closed, attributes are quoted, and special characters are escaped (e.g., `&lt;` for `<`, `&amp;` for `&`).
ReferenceError: require is not defined
This error indicates that you are attempting to use the CommonJS `require` syntax in an ECMAScript Module (ESM) environment (e.g., a modern React Native setup configured for ESM) that does not automatically provide a global `require` function.
fix
Switch to the ESM import syntax for `react-xml-parser`: `import XMLParser from 'react-xml-parser';`. If your project is strictly ESM, you might need to adjust your build configuration or use a tool like `createRequire` in Node.js, though the standard `import` is generally preferred for frontend React Native.
Upgrade
Version history
1.1.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
react-xml-parser — npm install react-xml-parser · libregistry