How do I parse an HTML file in React Native?

后端 未结 3 841
借酒劲吻你
借酒劲吻你 2021-02-09 21:49

How can I get an HTML file from file system and parse specific elements from it.

For example, given the html snippet below, how can I extract the table content and rende

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 22:34

    Get html with fetch() and parse with react-native-html-parser, process and display with WebView.

    import DOMParser from 'react-native-html-parser';
    
    fetch('http://www.google.com').then((response) => {
       const html = response.text();    
       const parser = new DOMParser.DOMParser();
       const parsed = parser.parseFromString(html, 'text/html');
       parsed.getElementsByAttribute('class', 'b');
    });
    

    P.S. fast-html-parser from other answers didn't work for me. I got multiple errors while installing it with react-native 0.54.

提交回复
热议问题