0

有人可以在 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)
}

谢谢。

4

1 回答 1

3

您可能想通过以下链接获取。 http://homepages.ius.edu/RWISMAN/C490/html/nsxmlparser.htm 它甚至在页面下方有一个示例。上面的解析应该足够简单了。更深入地了解您的问题究竟是什么将有所帮助。

于 2012-10-22T07:28:53.333 回答