0

I has an XML file like this, I am using NSXMLParser to parse it:

<? xml version ='1.0' encoding="UFT-8"?>
<!DOCTYPE plist PUBLIC "-//apple//DTD PLIST 1.0//EN" "http://www.apple/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<Data>
   <item>
     <date>2012-07-24</date>
     <name>A</name>
     <count>100</count>
     <startDate>2012-07-24</startDate>
     <stopDate>2012-07-24</stopDate>    
   </item>
   <item>
     <date>2012-07-24</date>
     <name>A</name>
     <count>100</count>
     <startDate>2012-07-24</startDate>
     <stopDate>2012-07-24</stopDate>
   </item>

</Data>
</plist>

use a IBAtion to parse the xml file like

    - (IBAction)Parser:(id)sender {
    NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[[NSBundle mainBundle]      URLForResource:@"file2" withExtension:@"xml"]];
    [xmlParser setDelegate:self];
    BOOL flag =[xmlParser parse];
    if (flag) {
        NSLog(@"OK");
    } else {
        NSLog(@"false");
    }
    [xmlParser release];
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:    (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

{
     NSLog(@"%@found a %@ element",self,elementName);
     if ([elementName isEqual:@"Data"]) {
        [data release];
        data = [[Data alloc]init];

    [data setParentParserDelegate:self];
    [parser setDelegate:data];
}

}

There is printf "OK" and no printf "NSLog(@"%@found a %@ element",self,elementName)"

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
tony
  • 15
  • 6

2 Answers2

0

NSXMLParser is stream-based, and sends various messages to its delegate when it encounters particular components in the XML stream - an opening tag, text, an end tag, and so forth. So, create an instance of NSXMLParser, and assign to it a delegate that you write that conforms to the NSXMLParserDelegate protocol. (Edit: From the code you added to your question, it looks like you may have already done this part, or at least made a good start on it.)

In your delegate class' -parser:didStartElement:... method, check the element name. If it's "Message", create a new NSMutableDictionary to hold the information in its child elements, and store that in a property: (Edit: In your code, it looks like you've declared the instance variable, but you should use dot-syntax to access it as a property, to make sure the correct memory-management happens.)

self.listOfFriends = [NSMutableDictionary dictionary];

For other element names, create an empty NSMutableString that will be used to hold text, and store that in a property:

self.thisString = [NSMutableString string];

In your -parser:foundCharacters: method, append the string to the mutable string property you created earlier.

[self.thisString appendString:string];

Finally, in your -parser:didEndElement:... method, check the element name. If it's "Message," you're done with this message element. If your XML stream has only one Message element (as in the example), you can now print the dictionary you've built, or whatever else it is you wanted to do with it. If there can be multiple Message elements, you could add the just-completed one to an array.

If the just-ended element is not a Message, add it to the dictionary with -setValue:forKey: passing the element name as a key, and the built-up string you created while receiving earlier -parser:foundCharacters: messages as the value.

[self.listOfFriends setValue:self.thisString forKey:elementName];
wackytacky99
  • 614
  • 1
  • 14
  • 33
  • now I confuse that if it was well parsing with NSXMLParser.NSLog(@"%@found a %@ element",self,elementName) was not printf – tony Jul 26 '12 at 06:25
0

I have found two problems in your code:

First, the xml is wrong. the very first line should be:

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

Second, the parser construction is wrong. basically you are initializing the URL in wrong way. It should be:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file2" ofType:@"xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];

With these two changes made to your code, It runs perfectly.

Hope this Helps.

Max
  • 3,371
  • 2
  • 29
  • 30
  • Thanks,With these two changes,it still no printf " NSLog(@"%@found a %@ element",self,elementName)",that is make me confused – tony Jul 26 '12 at 06:21
  • I found that if download the data from web it can well parsing! – tony Jul 26 '12 at 09:32
  • see the NSURL construction. instead of `[NSURL URLWithPath:filePath]` use `[NSURL fileURLWithPath:filePath]` for local file. In web url, your mothod should work. – Max Jul 26 '12 at 10:10
  • console printf 2012-07-26 22:03:20.582 XMlDemo[1811:f803] error! Error Domain=NSXMLParserErrorDomain Code=46 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 46.)" – tony Jul 26 '12 at 15:08