если вы используете TFHpple, вам нужно добавить следующий код в
DictionaryForNode
метод
Класс XPathQuery
if (XML_ELEMENT_NODE != currentNode->type) {
return nil;
}
Итак, ваш последний метод должен выглядеть как
NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult,BOOL parentContent)
{
NSMutableDictionary *resultForNode = [NSMutableDictionary dictionary];
if (currentNode->name) {
NSString *currentNodeContent = [NSString stringWithCString:(const char *)currentNode->name
encoding:NSUTF8StringEncoding];
resultForNode[@"nodeName"] = currentNodeContent;
}
if (XML_ELEMENT_NODE != currentNode->type) {
return nil;
}
xmlChar *nodeContent = xmlNodeGetContent(currentNode);
if (nodeContent != NULL) {
NSString *currentNodeContent = [NSString stringWithCString:(const char *)nodeContent
encoding:NSUTF8StringEncoding];
if ([resultForNode[@"nodeName"] isEqual:@"text"] && parentResult) {
if (parentContent) {
NSCharacterSet *charactersToTrim = [NSCharacterSet whitespaceAndNewlineCharacterSet];
parentResult[@"nodeContent"] = [currentNodeContent stringByTrimmingCharactersInSet:charactersToTrim];
xmlFree(nodeContent);
return nil;
}
if (currentNodeContent != nil) {
resultForNode[@"nodeContent"] = currentNodeContent;
}
return resultForNode;
} else {
resultForNode[@"nodeContent"] = currentNodeContent;
}
xmlFree(nodeContent);
}
xmlAttr *attribute = currentNode->properties;
if (attribute) {
NSMutableArray *attributeArray = [NSMutableArray array];
while (attribute) {
NSMutableDictionary *attributeDictionary = [NSMutableDictionary dictionary];
NSString *attributeName = [NSString stringWithCString:(const char *)attribute->name
encoding:NSUTF8StringEncoding];
if (attributeName) {
attributeDictionary[@"attributeName"] = attributeName;
}
if (attribute->children) {
NSDictionary *childDictionary = DictionaryForNode(attribute->children, attributeDictionary, true);
if (childDictionary) {
attributeDictionary[@"attributeContent"] = childDictionary;
}
}
if ([attributeDictionary count] > 0) {
[attributeArray addObject:attributeDictionary];
}
attribute = attribute->next;
}
if ([attributeArray count] > 0) {
resultForNode[@"nodeAttributeArray"] = attributeArray;
}
}
xmlNodePtr childNode = currentNode->children;
if (childNode) {
NSMutableArray *childContentArray = [NSMutableArray array];
while (childNode) {
NSDictionary *childDictionary = DictionaryForNode(childNode, resultForNode,false);
if (childDictionary) {
[childContentArray addObject:childDictionary];
}
childNode = childNode->next;
}
if ([childContentArray count] > 0) {
resultForNode[@"nodeChildArray"] = childContentArray;
}
}
xmlBufferPtr buffer = xmlBufferCreate();
xmlNodeDump(buffer, currentNode->doc, currentNode, 0, 0);
NSString *rawContent = [NSString stringWithCString:(const char *)buffer->content encoding:NSUTF8StringEncoding];
if (rawContent != nil) {
resultForNode[@"raw"] = rawContent;
}
xmlBufferFree(buffer);
return resultForNode;
}