0

I generate a html of an img tag is wrapped by 255 div tags. I use libxml parse the html and output the result, but the img tag is missed in the result. But if the count of the div tags is 254, the output result is right, the img tag is show in it. Why it happend and How to fix it? Is the node level too deep? The code is below:

    NSString* html = @"<img src='https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png'>";
    for (int i = 0; i < 255; i++) {
        html = [NSString stringWithFormat:@"<div>%@</div>", html];
    }
    NSData* data = [html dataUsingEncoding:NSUTF8StringEncoding];

    xmlDocPtr _doc = htmlReadMemory([_data bytes], (int)[_data length], "", NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
    if (_doc == NULL) {
        NSLog(@"Unable to parse.");
        return;
    }
    
    xmlXPathContextPtr _context = xmlXPathNewContext(_doc);
    if(_context == NULL) {
        NSLog(@"Unable to create XPath context.");
        return;
    }
    
    xmlNodePtr rootNode = xmlDocGetRootElement(_doc);
    xmlBufferPtr buffer = xmlBufferCreate();
    if (rootNode == NULL || rootNode == nil || buffer == NULL || buffer == nil) {
        return;
    }
    xmlNodeDump(buffer, rootNode->doc, rootNode, 0, 0);
    NSString *htmlContent = [NSString stringWithCString:(const char *)buffer->content encoding:NSUTF8StringEncoding];
    xmlBufferFree(buffer);

    xmlXPathFreeContext(_context);
    xmlFreeDoc(_doc);

    NSLog(@“%@”, htmlContent);

i try many different tags, but is the same way. Maybe the node level is too deep?

0

1 Answer 1

1

By default, the maximum depth of the document tree is indeed limited to 256. You should get an error message like:

Excessive depth in document: 256 use XML_PARSE_HUGE option

As the message explains, you get around this limitation by using the XML_PARSE_HUGE parser option.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, you are right! I delete the HTML_PARSE_NOERROR and then the console output the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.