gpt4 book ai didi

ios - NSXMLParser 输出 "XMLElement: 0x8d61200"但想要 "XMLElement: Richard"

转载 作者:行者123 更新时间:2023-11-29 12:59:23 28 4
gpt4 key购买 nike

我正在尝试弄清楚 NSXMLParser,但我不确定为什么它不起作用。我应该输出名字和姓氏以及年龄,但它输出的是一个数字。

XML 是

<?xml version="1.0" encoding="UTF-8"?>

<root>

<person id="1">

<firstName>Anthony</firstName>

<lastName>Robbins</lastName>

<age>51</age>

</person>

<person id="2">

<firstName>Richard</firstName>

<lastName>Branson</lastName>

<age>61</age>

</person>

</root>

在我的 AppDelegate.m 中有

#import "AppDelegate.h"
#import "XMLElement.h"

@interface AppDelegate () <NSXMLParserDelegate>

@property (nonatomic, strong) NSXMLParser *xmlParser;

@property (nonatomic, strong) XMLElement *rootElement;

@property (nonatomic, strong) XMLElement *currentElementPointer;

@end



@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"MyHTML"
ofType:@"HTML"];

NSData *xml = [[NSData alloc] initWithContentsOfFile:xmlFilePath];

self.xmlParser = [[NSXMLParser alloc] initWithData:xml];

self.xmlParser.delegate = self;

if ([self.xmlParser parse]){

NSLog(@"The XML is parsed.");


/* self.rootElement is now the root element in the XML */

XMLElement *element = self.rootElement.subElements[1];

NSLog(@"%@", element.subElements);



} else{

NSLog(@"Failed to parse the XML");

}

self.window = [[UIWindow alloc] initWithFrame:

[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

return YES;

}

- (void)parserDidStartDocument:(NSXMLParser *)parser{

self.rootElement = nil;

self.currentElementPointer = nil;

}

- (void) parser:(NSXMLParser *)parser

didStartElement:(NSString *)elementName

namespaceURI:(NSString *)namespaceURI

qualifiedName:(NSString *)qName

attributes:(NSDictionary *)attributeDict{

if (self.rootElement == nil){

/* We don't have a root element. Create it and point to it */

self.rootElement = [[XMLElement alloc] init];

self.currentElementPointer = self.rootElement;

} else {

/* Already have root. Create new element and add it as one of

the subelements of the current element */

XMLElement *newElement = [[XMLElement alloc] init];

newElement.parent = self.currentElementPointer;

[self.currentElementPointer.subElements addObject:newElement];

self.currentElementPointer = newElement;

}

self.currentElementPointer.name = elementName;

self.currentElementPointer.attributes = attributeDict;

}


- (void) parser:(NSXMLParser *)parser

foundCharacters:(NSString *)string{

if ([self.currentElementPointer.text length] > 0){

self.currentElementPointer.text =

[self.currentElementPointer.text stringByAppendingString:string];

} else {

self.currentElementPointer.text = string;

}
}


- (void) parser:(NSXMLParser *)parser

didEndElement:(NSString *)elementName

namespaceURI:(NSString *)namespaceURI

qualifiedName:(NSString *)qName{



self.currentElementPointer = self.currentElementPointer.parent;
}


- (void)parserDidEndDocument:(NSXMLParser *)parser{

self.currentElementPointer = nil;

}

在我的 XMLElement 类中是

 #import "XMLElement.h"
@implementation XMLElement


- (NSMutableArray *) subElements{

if (_subElements == nil){

_subElements = [[NSMutableArray alloc] init];

}

return _subElements;

}
@end

这是我的问题

2013-11-21 15:59:53.216 XML[7595:70b] 解析 XML。2013-11-21 15:59:53.219 XML[7595:70b] (“XML元素:0x8d61200”,“XML元素:0x8d61270”,“XMLElement:0x8d612e0”)2013-11-21 15:59:53.223 XML[7595:70b] 应用程序窗口预计在应用程序启动结束时有一个 Root View Controller

我想得到

2013-11-21 15:59:53.216 XML[7595:70b] 解析 XML。2013-11-21 15:59:53.219 XML[7595:70b] (“XMLElement:理查德”,“XMLElement:布兰森”,“XML元素:61”)2013-11-21 15:59:53.223 XML[7595:70b] 应用程序窗口预计在应用程序启动结束时有一个 Root View Controller

最佳答案

这是你设置它的方式:

#import "XMLElement.h"
@implementation XMLElement

- (NSMutableArray *) subElements {
if (_subElements == nil){
_subElements = [[NSMutableArray alloc] init];
}
return _subElements;
}

- (NSString *)description {
return [NSString stringWithFormat:"XMLElement: %@", self.text];
}

@end

关于ios - NSXMLParser 输出 "XMLElement: 0x8d61200"但想要 "XMLElement: Richard",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20197255/

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