Skip to content

Instantly share code, notes, and snippets.

@lexborisov
Created October 19, 2020 19:35
Show Gist options
  • Save lexborisov/304034f2ff6eebfbb99c4e6988b17609 to your computer and use it in GitHub Desktop.
Save lexborisov/304034f2ff6eebfbb99c4e6988b17609 to your computer and use it in GitHub Desktop.
#include <lexbor/html/html.h>
#include <lexbor/core/fs.h>
#define FAILED(...) \
do { \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(EXIT_FAILURE); \
} \
while (0)
#define PRINT(...) \
do { \
fprintf(stdout, __VA_ARGS__); \
fprintf(stdout, "\n"); \
} \
while (0)
int
main(int argc, const char *argv[])
{
size_t len;
lxb_char_t *text;
lxb_status_t status;
lxb_dom_element_t *body, *content_root;
lxb_html_document_t *document;
lxb_dom_collection_t *collection;
size_t html_len;
const lxb_char_t *html = lexbor_fs_file_easy_read((lxb_char_t *) "/Users/alexanderborisov/new/test.html", &html_len);
if (html == NULL) {
return EXIT_FAILURE;
}
/* Initialization */
document = lxb_html_document_create();
if (document == NULL) {
FAILED("Failed to create HTML Document");
}
/* Parse HTML */
status = lxb_html_document_parse(document, html, html_len);
if (status != LXB_STATUS_OK) {
FAILED("Failed to parse HTML");
}
collection = lxb_dom_collection_make(&document->dom_document, 128);
if (collection == NULL) {
FAILED("Failed to create Collection object");
}
body = lxb_dom_interface_element(document->body);
if (body == NULL) {
FAILED("Body is NULL");
}
status = lxb_dom_elements_by_attr(body, collection,
(lxb_char_t *) "id", 2,
(lxb_char_t *) "content", 7, true);
if (status != LXB_STATUS_OK) {
FAILED("Failed to get elements");
}
if (lxb_dom_collection_length(collection) == 0) {
FAILED("Elements not found");
}
content_root = lxb_dom_collection_element(collection, 0);
text = lxb_dom_node_text_content(&content_root->node, &len);
if (text == NULL) {
FAILED("Failed to create text");
}
PRINT("%.*s", (int) len, (const char *) text);
lxb_dom_collection_destroy(collection, true);
lxb_html_document_destroy(document);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment