NSMutableArray 和 NSDictionary 用于解析的 xml 数据 [英] NSMutableArray and NSDictionary using for parsed xml data

查看:40
本文介绍了NSMutableArray 和 NSDictionary 用于解析的 xml 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请任何人帮助我.我遇到了很大的麻烦......现在我的问题是......

Please anybody help me.i am in great trouble....Now my Problem is...

使用 xml 解析器,我解析了一些属性和属性值.

Using xml parser i have parsed some attribute and value of the attribute.

使用这个我得到元素名称:

using this i am getting the element name:

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

现在使用这个我从xml中获取属性的值:

Now using this i am getting value of the attribute from the xml:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
NSLog(@"found characters: %@ %@", currentElement,string);

假设我的 xml 数据是这样的...

suppose my xml data are something like that...

-<list>
−<ProductData HASH="838557843">
<id>1</id>
<productNumber>a91cc0f4c7</productNumber>
<name>Product 1</name>
<seoTitle>product-1</seoTitle>
<viewCount>0</viewCount>
<lowStock>0.0</lowStock>
<image>5e928bbae358c93caedf6115fa7d178b.jpg</image>
<dateCreated>2011-10-06T16:08:45</dateCreated>
<dateUpdated>2011-10-06T10:08:45</dateUpdated>
<isDummy>true</isDummy>
<isInventory>false</isInventory>
</ProductData>
-<ProductData HASH="681596439">
<id>2</id>
<productNumber>d8287e2e51</productNumber>
<name>Product 2</name>
<seoTitle>product-2</seoTitle>
−<description>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,....</p>
</description>
<viewCount>0</viewCount>
<availableStock>100.0</availableStock>
<lowStock>0.0</lowStock>
<image>8bbd8dfff3cdd28285d07810a4fe7c32.jpg</image>
<basePrice>10.0</basePrice>
<costPrice>10.0</costPrice>
<height>1.0</height>
<width>1.0</width>
<depth>1.0</depth>
<weight>3.0</weight>
<status>A</status>
<quantityOrderMin>1.0</quantityOrderMin>
<productIsCall>false</productIsCall>
<quantityOrderMax>20.0</quantityOrderMax>
<priceByAttribute>false</priceByAttribute>
<dateCreated>2011-10-06T16:08:45</dateCreated>
<dateUpdated>2011-10-06T10:08:45</dateUpdated>
<isDummy>true</isDummy>
<isInventory>false</isInventory>
</ProductData>
</list>`

现在,当我获得 id 属性时,它将存储在一个数组中,然后是第一个 id 之后的所有属性,直到下一个 id我想将属性及其值存储在字典中,当我将获得第二个 id 然后我想继续之前的过程......然后根据 id 的值我想在 UIViewUITableView 中显示值.它不是我想要显示的地方.

Now when i will get the id attribute then it will store in an array then all of the attribute after first id until next id i want to store the attribute and its value in the dictionary,when i will get second id then i want to continue previous process...then according to the value of id i want to display the value in a UIView or UITableView. its not neccesary where i want to display it.

我的问题是如何将数据存储在 arraydictionary 中并在我的 viewcontroller 中随时显示.请帮我.这对我来说是一个很大的麻烦.如果可以,请给我一个 示例代码 作为示例.请任何人帮助我...

My question is how can i store data in array and dictionary and display it any time in my viewcontroller. Please help me. its becoming a great trouble for me. if you can please give me a sample code as an example. please please anybody help me...

提前致谢

推荐答案

如果您将 XML 更改为

If you change your XML to

<list>
 <Object>
     <id>1</id>
     <product>name</product>
     <image>imagename.jpg</image>
     <dateCreated>date</dateCreated>
     <productleft>10</productleft>
 </Object>

 <Object>
     <id>2</id>
     <product>name</product>
     <image>imagename.jpg</image>
     <dateCreated>date</dateCreated>
     <productleft>30</productleft>
 </Object>
</list>

或者,如果您的 XML 看起来像上面那样易于使用的 SAX 解析器(您使用过的 NSXMLParser).否则,如果您想坚持使用已发布的 XML,请使用 DOM 样式解析.

Or if your XML looks something like above its easy to use SAX parser ( NSXMLParser you've used ). Otherwise if you want to stick to XML you have posted use DOM style parsing.

这是使用 NSXMLParser 格式化后的 XML 代码.

Here is the code for the XML after formatting using NSXMLParser.

// these are ivars
NSMutableArray * objectsArray;
NSMutableDictionary * productDict;
NSMutableString * currentString;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"Object"])
    {
        objectsArray = [[NSMutableArray alloc] init];
        productDict = [[NSMutableDictionary alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(!currentString)
    {
        currentString = [[NSMutableString alloc] init];
    }
    [currentString appendString:string]; 
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"id"])
    {
        [productDict setObject:currentString forKey:@"id"];
        [currentString release],currentString = nil;
        return;
    }
    if([elementName isEqualToString:@"product"])
    {
        [productDict setObject:currentString forKey:@"product"];
        [currentString release],currentString = nil;
        return;
    }
    if([elementName isEqualToString:@"image"])
    {
        [productDict setObject:currentString forKey:@"image"];
        [currentString release],currentString = nil;
        return;
    }
    if([elementName isEqualToString:@"dateCreated"])
    {
        [productDict setObject:currentString forKey:@"dateCreated"];
        [currentString release],currentString = nil;
        return;
    }
    if([elementName isEqualToString:@"productleft"])
    {
        [productDict setObject:currentString forKey:@"productleft"];
        [currentString release],currentString = nil;
        return;
    }
    if([elementName isEqualToString:@"Object"])
    {
        [objectsArray addObject:productDict];
        [productDict release],productDict = nil;
    }
    [currentString release], currentString = nil;
}

这篇关于NSMutableArray 和 NSDictionary 用于解析的 xml 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆