IPhoneDevelopmentNSXMLParserAnalysisXmlThe document tutorial is the content to be introduced in this article. This article is mainly implemented by code. Let's look at the code.XmlThe file is as follows: Question. xml, which is placed in the Resource file directory.
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Root>
- <Question>
- <QuestionTitle> what are your academic scores? </QuestionTitle>
- <Answers>
- <Answer>
- <Result> the primary account has a good score. </Result>
- <Evaluate> you must answer your score truthfully. </Evaluate>
- </Answer>
- <Answer>
- <Result> probably in the middle-to-top level </Result>
- <Evaluate> If the score is not good, the reason should be stated. </Evaluate>
- </Answer>
- <Answer>
- <Result> excellent score, top in our class </Result>
- <Evaluate> the attitude towards learning should be serious. </Evaluate>
- </Answer>
- </Answers>
- </Question>
- </Root>
-
- The IVPaserXMLSingleton. h file is as follows:
-
- # Import <Foundation/Foundation. h>
- # Import "IVQuestionModel. h"
- # Import "IVAnswerModel. h"
-
- @ Interface IVPaserXMLSingleton: NSObject {
- NSXMLParser * m_parser; // xml parsing class
- IVQuestionModel * m_queModel; // problem encapsulation class
- IVAnswerModel * m_ansModel; // encapsulation class of the answer
- NSMutableArray * m_arrXMLNode; // The encapsulated array list after xml Parsing
- }
-
- // Method for achieving the single benefit
- + (IVPaserXMLSingleton *) GetInstance;
-
- // Parse the xml file and encapsulate the parsed result into the list to return
- -(NSMutableArray *) GetArrayByPaserXML;
-
- @ End
-
- The IVPaserXMLSingleton. m file is a singleton class.
-
- @ Implementation IVPaserXMLSingleton
-
- Static IVPaserXMLSingleton * instance; // a singleton object
- BOOL flag = YES; // whether it is a repeated value assigned to a node
- NSString * m_strCurrentElement; // name of the read current node
-
- + (IVPaserXMLSingleton *) GetInstance {
- @ Synchronized (self ){
- If (instance = nil ){
- Instance = [[self alloc] init];
- }
- }
- Return instance;
- }
-
- -(NSMutableArray *) GetArrayByPaserXML {
- // Obtain the xml file
- NSString * pathpath = path = [[NSBundle mainBundle] pathForResource: @ "Question" ofType: @ "xml"];
- NSFileHandle * file = [NSFileHandle fileHandleForReadingAtPath: path];
- NSData * data = [file readDataToEndOfFile];
- [File closeFile];
-
- M_parser = [[NSXMLParser alloc] initWithData: data];
-
- // Set this class as a proxy class
- [M_parser setDelegate: self];
-
- BOOL flag = [m_parser parse];
- If (flag ){
- NSLog (@ "succeeded in obtaining the xml file in the specified path ");
- } Else {
- NSLog (@ "failed to get the xml file in the specified path ");
- }
- [M_parser release];
- Return m_arrXMLNode;
- }
-
- -(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName
- NamespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName
- Attributes: (NSDictionary *) attributeDict {
- // Read the start label flag = YES
- Flag = YES;
-
- // Record the currently resolved Node
- M_strCurrentElement = elementName;
-
- //// Read the start tag of the xml and node.
- If ([elementName isEqualToString: @ "Root"]) {
- M_arrXMLNode = [[NSMutableArray alloc] init];
- }
-
- // Parse the xml file for the interview question
- If ([m_xmlType isw.tostring: @ "Question"]) {
- If ([elementName isinclutostring: @ "Question"]) {
- M_queModel = [[IVQuestionModel alloc] init];
- M_queModel.m_arrAnswers = [[NSMutableArray alloc] init];
- } Else if ([elementName isinclutostring: @ "Answer"]) {
- M_ansModel = [[IVAnswerModel alloc] init];
- }
- }
- }
-
- -(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {
- If (flag ){
- // Parse the xml file for the interview question
- If ([m_xmlType isw.tostring: @ "Question"]) {
- If ([m_strCurrentElement isinclutostring: @ "QuestionTitle"]) {
- M_queModel.m_strQuestionTitle = string;
- } Else if ([m_strCurrentElement isinclutostring: @ "Result"]) {
- M_ansModel.m_strResult = string;
- } Else if ([m_strCurrentElement isinclutostring: @ "Evaluate"]) {
- M_ansModel.m_strEvaluate = string;
- }
- }
- }
- }
- -(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {
- // Read the end label flag = NO
- Flag = NO;
-
- // Parse the xml file for the interview question
- If ([m_xmlType isw.tostring: @ "Question"]) {
- If ([elementName isinclutostring: @ "Question"]) {
- [M_arrXMLNode addObject: m_queModel];
- [[M_queModel m_arrAnswers] release];
- [M_queModel release];
- M_queModel = nil;
- }
- If ([elementName isinclutostring: @ "Answer"]) {
- [[M_queModel m_arrAnswers] addObject: m_ansModel];
- [M_ansModel release];
- M_ansModel = nil;
- }
- }
- }
-
- -(Void) parserDidStartDocument :( NSXMLParser *) parser {
- NSLog (@ "Start parsing xml files ");
- }
-
- -(Void) parserDidEndDocument :( NSXMLParser *) parser {
- NSLog (@ "xml file parsed ");
- }
- @ End
The general process is like this. Common problems:
1. If an xml file fails to be read, the xml file format may be incorrect. Open the xml file with ie in the window to quickly check whether the xml file format is incorrect.
2. If the node value is too long, such as <Result>... </Result> if there is a long piece of content in the middle, the parsing process will cut the content into several segments for reading, namely:-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) the string method will be repeatedly called until all the content is read, so if you use: m_ansModel.m_strResult = string; in this way, assign values, the result is incomplete. The solution is to use the NSMutableString type to receive the read value, like this:
- if (m_ansModel.m_strResult == nil) {
- m_ansModel.m_strResult = [[NSMutableString alloc] initWithString:string];
- } else {
- [m_ansModel.m_strResult appendString:string];
- }
M_strResult is of the NSMutableString type. OK.
The above is my summary and I hope it will help you.
Summary:IPhoneDevelopmentNSXMLParserAnalysisXMLThe content of the document tutorial is complete. I hope this article will help you!