我试图找出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](
“XMLElement:0x8d61200”,
“XMLElement:0x8d61270”,
“XMLElement:0x8d612e0”
)
2013-11-21 15:59:53.223 XML [7595:70b]应用程序窗口应在应用程序启动结束时具有根视图控制器

我想得到

2013-11-21 15:59:53.216 XML [7595:70b]已解析XML。
2013-11-21 15:59:53.219 XML [7595:70b](
“XMLElement:Richard”,
“XMLElement:布兰森”,
“XMLElement:61”
)
2013-11-21 15:59:53.223 XML [7595:70b]应用程序窗口应在应用程序启动结束时具有根视图控制器

最佳答案

设置方法如下:

#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/

10-09 19:25