Load a Document from a File(从一个文件中加载文档)

[size=large]Problem[/size]
You have a file on disk that contains HTML(你有一个文件他包含html), that you'd like to load and parse(你想加载并解析它), and then maybe manipulate or extract data from(并操作的它或者获得他的数据).

[size=large]Solution[/size]
Use the static Jsoup.parse(File in, String charsetName, String baseUri) method(使用静态Jsoup.parse(File in, String charsetName, String baseUri)方法):

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

[size=large]Description[/size]
The parse(File in, String charsetName, String baseUri) method loads and parses a HTML file(这个arse(File in, String charsetName, String baseUri)方法加载并解析一个HTML文件). If an error occurs whilst loading the file(如果加载的文件有个错误), it will throw an IOException(会抛出IOException错误), which you should handle appropriately(你用合理的方法处理它).

The baseUri parameter is used by the parser to resolve relative URLs in the document before a <base href> element is found(baseUri用参数的解析器能够解析相对url的文档). If that's not a concern for you, you can pass an empty string instead(如果你不需要可以传你个空字符串).

There is a sister method parse(File in, String charsetName) which uses the file's location as the baseUri(它的姐妹方法parse(File in, String charsetName)它使用文件的位置baseUri吗). This is useful if you are working on a filesystem-local site and the relative links it points to are also on the filesystem(这是有用的,如果你工作在文件系统的本地站点和相对链接,它指向也在文件系统。).
A:好的,以下是一个简单的文档管理器实现,可以存储一些文档模板。它使用C语言的文件操作函数来读取和写入文档,以及链表来存储文档信息。 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Document { char name[100]; char content[1000]; struct Document* next; } Document; Document* head = NULL; void addDocument() { Document* newDoc = (Document*) malloc(sizeof(Document)); printf("Enter document name: "); fgets(newDoc->name, 100, stdin); printf("Enter document content: "); fgets(newDoc->content, 1000, stdin); newDoc->next = head; head = newDoc; } void saveDocuments() { FILE* file = fopen("documents.txt", "w"); if (file == NULL) { printf("Error: could not open file\n"); return; } Document* current = head; while (current != NULL) { fprintf(file, "%s\n", current->name); fprintf(file, "%s\n", current->content); current = current->next; } fclose(file); } void loadDocuments() { FILE* file = fopen("documents.txt", "r"); if (file == NULL) { printf("Error: could not open file\n"); return; } char name[100]; char content[1000]; while (fgets(name, 100, file) != NULL) { fgets(content, 1000, file); Document* newDoc = (Document*) malloc(sizeof(Document)); strcpy(newDoc->name, name); strcpy(newDoc->content, content); newDoc->next = head; head = newDoc; } fclose(file); } void printDocuments() { Document* current = head; while (current != NULL) { printf("Name: %s", current->name); printf("Content: %s", current->content); current = current->next; } } int main() { loadDocuments(); // load existing documents from file printf("Document Manager\n"); printf("----------------\n"); while (1) { printf("1. Add document\n"); printf("2. Save documents\n"); printf("3. Print documents\n"); printf("0. Exit\n"); int choice; printf("Enter choice: "); scanf("%d", &choice); getchar(); // consume newline left by scanf switch (choice) { case 1: addDocument(); break; case 2: saveDocuments(); break; case 3: printDocuments(); break; case 0: exit(0); default: printf("Invalid choice\n"); } printf("\n"); } return 0; } ``` 这个管理器允许用户添加,打印和保存文档。当程序运行时,它会自动加载之前保存的文档,如果存在的话。 所有文档会在程序退出时保存到磁盘上,以便下次使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值