Trac is being migrated to new services! Issues can be found in our new
YouTrack instance and WIKI pages can be found on our
website.
Line | |
---|
1 | /* |
---|
2 | Public domain |
---|
3 | Compile with gcc `xml2-config --cflags --libs` -o xmlparser xmlparser.c |
---|
4 | */ |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <libxml/parser.h> |
---|
9 | |
---|
10 | #if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED) |
---|
11 | int |
---|
12 | main(int argc, char *argv[]) |
---|
13 | { |
---|
14 | xmlDocPtr doc; |
---|
15 | xmlChar *xmlbuf; |
---|
16 | int bufsize, i, indentspaces=1; |
---|
17 | xmlChar *ptr; |
---|
18 | |
---|
19 | if (argc != 2 && argc != 3) { |
---|
20 | fprintf(stderr, "Usage: %s <xmlfile> [number of indent spaces in multiples of 2 (default 1x)]\n",argv[0]); |
---|
21 | exit(EXIT_FAILURE); |
---|
22 | } |
---|
23 | if (argc == 3) |
---|
24 | indentspaces = atoi(argv[2]); |
---|
25 | |
---|
26 | doc = xmlReadFile(argv[1], NULL, 0); |
---|
27 | if (doc == NULL) { |
---|
28 | perror(argv[1]); |
---|
29 | exit(EXIT_FAILURE); |
---|
30 | } |
---|
31 | |
---|
32 | xmlDocDumpFormatMemory(doc, &xmlbuf, &bufsize, 1); |
---|
33 | |
---|
34 | if (indentspaces == 1) |
---|
35 | printf((char *) xmlbuf); |
---|
36 | else { |
---|
37 | for (ptr=xmlbuf ; ptr < xmlbuf + bufsize; ptr++) { |
---|
38 | fputc((int) *ptr, stdout); |
---|
39 | if ((char) *ptr == '\n') { |
---|
40 | ptr++; |
---|
41 | while ((char) *ptr == ' ' && ptr < xmlbuf + bufsize) { |
---|
42 | for (i = 0 ; i < indentspaces; i++) |
---|
43 | fputc((int) ' ', stdout); |
---|
44 | ptr++; |
---|
45 | } |
---|
46 | ptr--; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | xmlFree(xmlbuf); |
---|
53 | xmlFreeDoc(doc); |
---|
54 | |
---|
55 | xmlCleanupParser(); |
---|
56 | |
---|
57 | exit(EXIT_SUCCESS); |
---|
58 | } |
---|
59 | |
---|
60 | #else |
---|
61 | |
---|
62 | int |
---|
63 | main(void) |
---|
64 | { |
---|
65 | fprintf(stderr, "Your libxml2 installation needs to be compiled with tree and output support for this tool to work\nAborting.\n"); |
---|
66 | exit(EXIT_FAILURE); |
---|
67 | } |
---|
68 | |
---|
69 | #endif |
---|
Download in other formats:
All information, including names and email addresses, entered onto this website or sent to mailing lists affiliated with this website will be public. Do not post confidential information, especially passwords!