React Native HTML Parser

0.1.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates parsing an HTML string, querying elements using various methods like `querySelect` and `getElementsByTagName`, and briefly shows the document structure. Note that `XMLSerializer` import might need adjustment depending on the exact build environment.

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;

view raw JSON →