-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtxpathquery.cpp
145 lines (117 loc) · 3.84 KB
/
qtxpathquery.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "qtxpathquery.h"
#include <libxml/tree.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/HTMLparser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <QtCore>
#include <QMap>
//NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult,BOOL parentContent);
//NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query);
QtXMLElement* ElementForNode(xmlNodePtr currentNode)
{
// NSMutableDictionary *resultForNode = [NSMutableDictionary dictionary];
QString tagName;
QString content;
// QMap<QString, QString> resultForNode;
if (currentNode->name)
{
tagName = QString::fromUtf8((const char *)currentNode->name);
}
if (currentNode->content && currentNode->content != (xmlChar *)-1)
{
content = QString::fromUtf8((const char *)currentNode->content);
}
xmlAttr *attribute = currentNode->properties;
QMap<QString, QString>attributeMap;
while(attribute && attribute->name && attribute->children)
{
attributeMap[QString((const char*)attribute->name)] = QString::fromUtf8((const char*)attribute->children->content);
attribute = attribute->next;
}
xmlNodePtr childNode = currentNode->children;
QVector<QtXMLElement *> children;
if (childNode)
{
while (childNode)
{
children.append(ElementForNode(childNode));
childNode = childNode->next;
}
}
xmlBufferPtr buffer = xmlBufferCreate();
xmlNodeDump(buffer, currentNode->doc, currentNode, 0, 0);
QString raw = QString::fromUtf8((const char *)buffer->content);
xmlBufferFree(buffer);
return new QtXMLElement(raw, content, tagName, attributeMap, children);
}
QVector<QtXMLElement *> PerformXPathQuery(xmlDocPtr doc, QString query)
{
QVector<QtXMLElement *> result;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL)
{
// NSLog(@"Unable to create XPath context.");
throw "Unable to create XPath context.";
// return nil;
}
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression((xmlChar *)query.toLatin1().data(), xpathCtx);
if(xpathObj == NULL) {
// NSLog(@"Unable to evaluate XPath.");
xmlXPathFreeContext(xpathCtx);
throw "Unable to evaluate XPath.";
return result;
}
xmlNodeSetPtr nodes = xpathObj->nodesetval;
if (!nodes)
{
// NSLog(@"Nodes was nil.");
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
throw "Nodes was nil.";
return result;
}
for (int i = 0; i < nodes->nodeNr; i++)
{
result.append(ElementForNode(nodes->nodeTab[i]));
}
/* Cleanup */
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return result;
}
QVector<QtXMLElement *> PerformHTMLXPathQuery(QByteArray *document, QString query)
{
xmlDocPtr doc;
/* Load XML document */
doc = htmlReadMemory(document->data(), document->length(), "", "", HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
if (doc == NULL)
{
throw "Unable to parse.";
// NSLog(@"Unable to parse.");
// return result;
}
QVector<QtXMLElement *> result = PerformXPathQuery(doc, query);
xmlFreeDoc(doc);
return result;
}
QVector<QtXMLElement *> PerformXMLXPathQuery(QByteArray *document, QString query)
{
xmlDocPtr doc;
/* Load XML document */
doc = htmlReadMemory(document->data(), document->length(), "", "", XML_PARSE_RECOVER);
if (doc == NULL)
{
throw "Unable to parse.";
// NSLog(@"Unable to parse.");
// return result;
}
QVector<QtXMLElement *> result = PerformXPathQuery(doc, query);
xmlFreeDoc(doc);
return result;
}