适用于iOS的iPhone SAX XML Parser示例

时间:2012-10-22 07:12:09

标签: iphone ios xml xcode ios6

有人可以在XCode中发布SAX XML解析器示例的链接,而不是来自developer.apple.com。 我需要解析这个XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<userfood_overview xmlns="http://www.wikifood.lu"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wikifood.lu userfood.xsd">
<hits>1</hits>
<startAt>0</startAt>
<userfood>
<id>770</id>
<ean>4016249502058 </ean>
<name><![CDATA[Amaranth-Früchte-Müsli / Großpackung]]></name>
<modification>2012-10-09</modification>
<info1><![CDATA[Amaranth - Müsli mit Früchten]]></info1>
<origin id="55">Germany</origin>
<brand>
<id>155</id>
<name><![CDATA[Allos]]></name>
</brand>
<contents><![CDATA[1,5]]></contents>
<unit id="8">Kilogram</unit>
<nutrition_facts>
<nutrition_fact id="Per_100_g_ml">
<name>per 100</name>
<value>g</value>
<unit/>
</nutrition_fact>
<nutrition_fact id="EnergyKcal">
<name>Energy kcal</name>
<value>377</value>
<unit>kcal</unit>
</nutrition_fact>
<nutrition_fact id="EnergyKjoule">
<name>Energy kj</name>
<value>1580</value>
<unit>kj</unit>
</nutrition_fact>
</nutrition_facts>
<allergens>
<allergen id="774"><![CDATA[Nuts]]></allergen>
<allergen id="773"><![CDATA[Milk (including lactose)]]></allergen>
<allergen id="777"><![CDATA[Sesame seeds]]></allergen>
<allergen id="782"><![CDATA[Gluten]]></allergen>
<allergen id="772"><![CDATA[Soy]]></allergen>
</allergens>
<food_notes/>
<category id="0602003" delimiter=":"><![CDATA[Muesli]]></category>
<insertion>2007-01-16</insertion>
<editable>false</editable>
</userfood>
</userfood_overview>

,但是从http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html#//apple_ref/doc/uid/20002264-BCIIJEEH的文档来看,这对我来说并不清楚。 这就是我所拥有的:

#pragma mark *** NSXMLParserDelegate ***

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    // do preparation for loading data from XML
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    [parser release];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    //analyze parseError
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"elementName %@", elementName); // current parsing element
    // specify type of data to be saved in method below
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"value of element %@", string); // value for current parsing element
    // save data to some ivar for future use (displaying)
}

感谢。

1 个答案:

答案 0 :(得分:3)

您可能想要通过以下链接。 http://homepages.ius.edu/RWISMAN/C490/html/nsxmlparser.htm 它甚至在页面上有一个例子。上面的解析应该足够简单。更深入地了解您的问题到底有何帮助。