4

I had tried Jquery, Parse5, JsDom, and found that they can't work in nativescript. Jquery is Dom-dependent, and Parse5 and JsDom depend on Node.js which is not supported by nativescript now. What I want is only a html-parser, is it possible to import jquery into nativescript using as a hmtl-parser? If it's possible how can I make it.If not, Is there a handy html-parser can be used in Nativescript(With Angular2 + TypeScripyt)。

Details about my application. I am developping a mobile app for moodle use nativescript。My app communicates with moodle by moodle's rest api, and some content of it is html string. So I need a html-parser to get the things in that html-string.

For example, I send a "mod_quiz_get_attempt_data" request to moodle. And I will fetch a json response as below:

{"questions": [ { "slot": 1, "type": "multichoice", "page": 0, "html": "Html string here.Can be very complex.I can't post html-string, stackoverflow ignore them", } ] }

Some data I need is in the "html" part which is html-string.Because moodle is a third party,So I prefer to handle this in my app。

@Marek Maszay, @Emil Oberg

@Emil Oberg, I have given cheerio a try.It doesn't work.Because cheerio depends on htmlparser2 which is also depend on Nodejs.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
yoshiyan
  • 41
  • 3
  • Maybe you could find a C one and emscripten it? – Cauterite Sep 05 '16 at 09:09
  • What u want to do with html parser ? nativescript hasnt way to do with html because there arent html elements only natives one – Marek Maszay Sep 05 '16 at 10:33
  • @Marek Maszay, I am developping a mobile app for moodle use nativescript。My app communicates with moodle by moodle's rest api, and some content of it is html string. So I need a html-parser to get the things in that html-string. – yoshiyan Sep 05 '16 at 11:41
  • @Cauterite, thanks for your reply.Your suggestion is too heavy-weight for me. I want a ready-made html-parser can be used in nativescript. – yoshiyan Sep 05 '16 at 11:44

3 Answers3

0

As you've been looking at jQuery (among others) I'd say that Cheerio is what you want. It is an implementation of core jQuery designed for a non-DOM environment (such as in a NativeScript app, on the server, etc).

However, parsing HTML is commonly not something you need to do in a NativeScript app. Out of curiosity: What are you trying to do?

Emil Oberg
  • 4,006
  • 18
  • 30
0

I ran into a similar problem and used nativescript-xml2js to solve it.

It converts the html structure (tags, attrs) into JSON and works in Nativescript with Typescript or plain js.

nelsonec87
  • 106
  • 6
0

I succesfully used Cheerio in my Nativescript App the following way:

- npm i cheerio-without-node-native@0.20.2 // By Ouyang Yadong(https://github.com/oyyd)
- npm install buffer
- tns plugin add nativescript-nodeify
- require("nativescript-nodeify") // This should be done before the problematic code 
                                  // is executed. If working in an angular proyect, 
                                  // you can simply call it in your main.ts file 
                                  // before bootstrapping the main application's 
                                  // module. 
- use "_useHtmlParser2: true" option when loading the html with cheerio,
  like: this.$ = this.cheerio.load(siteRequested, { _useHtmlParser2: true }); or 
  const $ = cheerio.load(siteRequested, { _useHtmlParser2: true });

- If you're requesting your html using http, set responseType to text, as in 
  return this.http.get(url, {responseType: 'text'});

After this you can normally use the library. Hope it helps.

Alaska
  • 1
  • 1