-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPathQuery.m
More file actions
executable file
·155 lines (128 loc) · 4.28 KB
/
XPathQuery.m
File metadata and controls
executable file
·155 lines (128 loc) · 4.28 KB
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
145
146
147
148
149
150
151
152
153
154
155
//
// XPathQuery.m
// FuelFinder
//
// Created by Matt Gallagher on 4/08/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
// Modified by Saravudh Sinsomros on 25/10/2010.
#import "XPathQuery.h"
#import <libxml/tree.h>
#import <libxml/parser.h>
#import <libxml/HTMLparser.h>
#import <libxml/xpath.h>
#import <libxml/xpathInternals.h>
#import "SSNode.h"
SSNode *SSNodeForNode(xmlNodePtr currentNode, SSNode *parentNode)
{
if (currentNode->name)
{
NSString *currentNodeName = [NSString stringWithCString:(const char *)currentNode->name encoding:NSUTF8StringEncoding];
SSNode *resultNode = nil;
NSString *currentNodeContent = nil;
if (currentNode->content && currentNode->content != (xmlChar *)-1) {
currentNodeContent = [NSString stringWithCString:(const char *)currentNode->content encoding:NSUTF8StringEncoding];
currentNodeContent = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if ([currentNodeName isEqual:@"text"] && parentNode) {
[parentNode appendTextContent:currentNodeContent];
return nil;
} else {
resultNode = [SSNode node];
resultNode.name = currentNodeName;
[resultNode appendTextContent:currentNodeContent];
}
xmlAttr *attribute = currentNode->properties;
if (attribute) {
while (attribute) {
NSMutableDictionary *attributeDictionary = [NSMutableDictionary dictionary];
NSString *attributeName =
[NSString stringWithCString:(const char *)attribute->name encoding:NSUTF8StringEncoding];
if (attributeName) {
[attributeDictionary setObject:attributeName forKey:@"attributeName"];
}
if (attribute->children) {
if (attribute->children->content && attribute->children->content != (xmlChar *)-1) {
NSString *currentNodeContent = [NSString stringWithCString:(const char *)attribute->children->content encoding:NSUTF8StringEncoding];
currentNodeContent = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[attributeDictionary setObject:currentNodeContent forKey:@"attributeContent"];
}
}
if ([attributeDictionary count] > 0) {
[resultNode addAttribute:attributeDictionary];
}
attribute = attribute->next;
}
}
xmlNodePtr childNode = currentNode->children;
while (childNode) {
SSNode *childWrapperNode = SSNodeForNode(childNode,resultNode);
if (childWrapperNode) {
[resultNode appendChild:childWrapperNode];
}
childNode = childNode->next;
}
return resultNode;
}
return nil;
}
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query) {
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL) {
NSLog(@"Unable to create XPath context.");
return nil;
}
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression((xmlChar *)[query cStringUsingEncoding:NSUTF8StringEncoding], xpathCtx);
if(xpathObj == NULL) {
NSLog(@"Unable to evaluate XPath.");
return nil;
}
xmlNodeSetPtr nodes = xpathObj->nodesetval;
if (!nodes) {
NSLog(@"Nodes was nil.");
return nil;
}
NSMutableArray *resultNodes = [NSMutableArray array];
for (NSInteger i = 0; i < nodes->nodeNr; i++) {
SSNode *node = SSNodeForNode(nodes->nodeTab[i], nil);
if (node) {
[resultNodes addObject:node];
}
}
/* Cleanup */
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return resultNodes;
}
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query)
{
xmlDocPtr doc;
/* Load XML document */
doc = htmlReadMemory([document bytes], [document length], "", NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
if (doc == NULL)
{
NSLog(@"Unable to parse.");
return nil;
}
NSArray *result = PerformXPathQuery(doc, query);
xmlFreeDoc(doc);
return result;
}
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query)
{
xmlDocPtr doc;
/* Load XML document */
doc = xmlReadMemory([document bytes], [document length], "", NULL, XML_PARSE_RECOVER);
if (doc == NULL)
{
NSLog(@"Unable to parse.");
return nil;
}
NSArray *result = PerformXPathQuery(doc, query);
xmlFreeDoc(doc);
return result;
}