C HTML格式解析与生成

打印 上一主题 下一主题

主题 997|帖子 997|积分 3001

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
cmake报错替换 

   if(NOT MyHTML_BUILD_WITHOUT_THREADS OR NOT MyCORE_BUILD_WITHOUT_THREADS)
    set(CMAKE_THREAD_PREFER_PTHREAD 1)
    if (WIN32)
        set(CMAKE_USE_WIN32_THREADS_INIT ON)
        set(CMAKE_THREAD_PREFER_PTHREADS TRUE)
        set(THREADS_PREFER_PTHREAD_FLAG TRUE)
    else ()
        find_package(Threads REQUIRED)
        if(NOT CMAKE_USE_PTHREADS_INIT)
            message(FATAL_ERROR "Could NOT find pthreads (missing: CMAKE_USE_PTHREADS_INIT)")
        endif()
    endif()
endif() 
  测试

   add_executable(example examples/myhtml/modify_and_serialize.c)
target_link_libraries(example ${PROJECT_LIB_STATIC})
  代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <myhtml/serialization.h>
  5. /**
  6. *  Write output
  7. *  @param  buffer
  8. *  @param  size
  9. *  @param  ptr
  10. */
  11. mystatus_t write_output(const char* data, size_t len, void* ctx)
  12. {
  13.     printf("%.*s", (int)len, data);
  14.     return MyCORE_STATUS_OK;
  15. }
  16. /**
  17. *  Main procedure
  18. *  @return int
  19. */
  20. int main()
  21. {
  22.     // initalize html engine
  23.     myhtml_t *myhtml = myhtml_create();
  24.     myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0);
  25.    
  26.     // init tree
  27.     myhtml_tree_t *tree = myhtml_tree_create();
  28.     myhtml_tree_init(tree, myhtml);
  29.    
  30.     // input string
  31.     const char *input = "<html><head></head><body><a href=http://nu.nl></body></html>";
  32.    
  33.     // parse html
  34.     myhtml_parse(tree, MyENCODING_UTF_8, input, strlen(input));
  35.     // collection of links
  36.     myhtml_collection_t *collection = myhtml_get_nodes_by_name(tree, NULL, "a", 1, NULL);
  37.    
  38.     // iterate over all nodes
  39.     for (size_t i = 0; i < collection->length; ++i)
  40.     {
  41.         // add attribute
  42.         myhtml_attribute_add(collection->list[i], "title", 5, "my value", 8, MyENCODING_UTF_8);
  43.     }
  44.    
  45.     // write the document again
  46.     myhtml_serialization_tree_callback(myhtml_tree_get_document(tree), write_output, NULL);
  47.    
  48.     myhtml_collection_destroy(collection);
  49.     myhtml_tree_destroy(tree);
  50.     myhtml_destroy(myhtml);
  51.    
  52.     // done
  53.     return 0;
  54. }
复制代码
修改前

   "<html><head></head><body><a href=http://nu.nl></body></html>
  修改后

   <html><head></head><body><a href="http://nu.nl" title="my value"></a></body></html>
  参考

https://github.com/lexborisov/Modest
GitHub - lexborisov/myhtml: Fast C/C++ HTML 5 Parser. Using threads.


创作不易,小小的支持一下吧!



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

泉缘泉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表