-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyxml.cpp
executable file
·63 lines (54 loc) · 1.81 KB
/
myxml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#if defined(unix) || defined(__unix__) || defined(__unix)
#include<stdio.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include<libxml/parser.h>
#include <libxml/xpathInternals.h>
#include<assert.h>
#include "myxml.h"
#include<string>
#include<iostream>
using namespace std;
void print_node(xmlNode *n) {
std::cout << "named '" << n->name << "'";
const xmlChar *content = xmlNodeGetContent(n);
if (content)
std::cout << " with content '" << content << "'";
std::cout << "\n";
}
void print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output) {
xmlNodePtr cur;
int size;
int i;
assert(output);
size = (nodes) ? nodes->nodeNr : 0;
fprintf(output, "Result (%d nodes):\n", size);
for (i = 0; i < size; ++i) {
assert(nodes->nodeTab[i]);
if (nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns;
ns = (xmlNsPtr) nodes->nodeTab[i];
cur = (xmlNodePtr) ns->next;
if (cur->ns) {
fprintf(output, "= namespace \"%s\"=\"%s\" for node %s:%s\n",
ns->prefix, ns->href, cur->ns->href, cur->name);
} else {
fprintf(output, "= namespace \"%s\"=\"%s\" for node %s\n",
ns->prefix, ns->href, cur->name);
}
} else if (nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
cur = nodes->nodeTab[i];
if (cur->ns) {
fprintf(output, "= element node \"%s:%s\"\n",
cur->ns->href, cur->name);
} else {
fprintf(output, "= element node \"%s\"\n",
cur->name);
}
} else {
cur = nodes->nodeTab[i];
fprintf(output, "= node \"%s\": type %d\n", cur->name, cur->type);
}
}
}
#endif