React Native HTML Parser
This package provides a DOM/XML parser for React Native, Titanium, and browser environments, based on the `xmldom` library. The current and only stable version is 0.1.0, which was last published over 6 years ago. It aims to offer a W3C DOM Level 2 Core-compatible API, including `DOMParser` and `XMLSerializer`, along with `xmldom`-specific extensions for error handling. Due to its age and lack of maintenance, it is unlikely to be compatible with modern JavaScript, Node.js, or React Native environments and may contain unpatched security vulnerabilities inherited from its `xmldom` dependency. Developers should consider more actively maintained alternatives for parsing HTML in React Native.
Common errors
-
[xmldom error] entity not found: ~~~~~
cause This error occurs when the parser encounters an XML or HTML entity (e.g., ` `, `—`) that it does not recognize or that is malformed.fixCheck the HTML/XML source for invalid or unsupported entities. Ensure character encoding is correctly specified if external sources are being parsed. Refer to the linked GitHub issue for potential workarounds or to preprocess HTML.
Warnings
- breaking This package has been abandoned for over 6 years (last published v0.1.0). It is highly unlikely to be compatible with modern React Native, Node.js, or browser environments due to significant changes in these platforms.
- breaking The underlying `xmldom` library, upon which this package is based, has multiple known security vulnerabilities, including XML injection, prototype pollution, and improper input validation. The `xmldom` maintainers advise migrating to `@xmldom/xmldom` for security fixes.
- gotcha Examples in the README use deprecated React Native imports (e.g., `import { View, Text } from 'react-native'`). Modern React Native requires specific imports for components or hooks.
- gotcha The `xmldom` library, and thus `react-native-html-parser`, might not be fully spec-compliant with the latest HTML5 parsing rules, potentially leading to unexpected parsing behavior compared to browser-native `DOMParser` implementations.
Install
-
npm install react-native-html-parser -
yarn add react-native-html-parser -
pnpm add react-native-html-parser
Imports
- DOMParser
const DOMParser = require('react-native-html-parser').DOMParser;import { DOMParser } from 'react-native-html-parser'; - DOMParser
import DomParser from 'react-native-html-parser';
const DomParser = require('react-native-html-parser').DOMParser; - XMLSerializer
import { XMLSerializer } from 'react-native-html-parser';const { XMLSerializer } = require('react-native-html-parser');
Quickstart
import React, { Component } from 'react-native';
import { DOMParser } from 'react-native-html-parser';
class TestReactNativeHtmlParser extends Component {
componentDidMount() {
let html = `<html>
<body>
<div id="b a">
<a href="example.org">
<div class="inA">
<br>bbbb</br>
</div>
</div>
<div class="bb a">
Test
</div>
</body>
</html>`;
// Instantiate DOMParser and parse the HTML string
let doc = new DOMParser().parseFromString(html, 'text/html');
// Example usage of parsed document methods
console.log('QuerySelect #b .inA:', doc.querySelect('#b .inA'));
console.log('getElementsByTagName a:', doc.getElementsByTagName('a'));
console.log('QuerySelect #b a[href="example.org"]:', doc.querySelect('#b a[href="example.org"]'));
console.log('getElementsByClassName a (non-strict):', doc.getElementsByClassName('a', false));
// Serialize the document back to a string
// const serializer = new XMLSerializer(); // Not directly exported from main module in all cases
// console.log('Serialized HTML:', serializer.serializeToString(doc));
}
render() {
return null; // For demonstration, no UI rendered
}
}
export default TestReactNativeHtmlParser;