gpt4 book ai didi

objective-c - TBXML textForElement 问题

转载 作者:行者123 更新时间:2023-11-29 13:26:36 28 4
gpt4 key购买 nike

我正在尝试使用适用于 iOS 的 TBXML 库解析 HTML,并且我想获得这段 HTML 的“更多文本”值:

<div> 
<a href="/url/1">
<strong>value</strong>
</a>
more text
</div>

我用过这段代码,但它似乎不起作用:

//Assume that "div" is a TBXMLElement* for this div 
NSString* content = [TBXML textForElement:div];
//Returns @"" when the value @"more text" is expected...

我的代码有什么问题?

最佳答案

好的,我修改了 TBXML 库并解决了问题...如果有人遇到同样的问题,请尝试以下操作:

1) 在文件 TBXML.h 中为 TBXMLElement 创建一个名为 NSString* afterText 的属性。

2) 在文件 TBXML.m 中搜索这段代码并对其进行注释:

// if parent element has children clear text
if (parentXMLElement && parentXMLElement->firstChild)
parentXMLElement->text = 0;

3) 在步骤1的代码之前写这段代码:

if (parentXMLElement && parentXMLElement->firstChild){

//if the next string does not content...
const char * parentNametag = [[TBXML elementName:parentXMLElement] UTF8String];
char * finalTag = (char *)malloc(sizeof("</")+sizeof(parentNametag)+sizeof(">"));
strcpy(finalTag,"</");
strcat(finalTag,parentNametag);
strcat(finalTag,">");

char * elementTextStart = elementStart;//parentXMLElement->text;
char * elementTextEnd = elementTextStart;

elementTextEnd = strstr(elementStart,finalTag);

if(elementTextEnd != NULL){
long textLength = strlen(elementTextStart) - strlen(elementTextEnd) ;
if (textLength > 0){
afterTextStart = (char *)malloc(textLength*sizeof(char));
memcpy(afterTextStart, elementTextStart,(textLength*sizeof(char)));
parentXMLElement->afterText = afterTextStart;
}
}

}

现在属性“after text”包含“more text”

这不是一个正统的解决方案,但它对我有用。

关于objective-c - TBXML textForElement 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12977313/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com