NSMutableArray 和 NSDictionary 用于解析 xml 数据

发布于 2024-12-09 11:17:09 字数 2685 浏览 0 评论 0原文

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

使用 xml 解析器我已经解析了一些属性和属性的值。

使用这个我获取元素名称:

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

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

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

假设我的 xml 数据是类似的...

-<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 我想将属性及其值存储在字典中,当我得到第二个 < code>id 然后我想继续前面的过程...然后根据值我想在 UIViewUITableView 中显示 id 的值。它不需要在我想显示它的地方。

我的问题是如何将数据存储在数组字典中并随时在我的视图控制器中显示。请帮我。这对我来说成了一个很大的麻烦。如果可以的话,请给我一个示例代码作为示例。请任何人帮助我...

提前致谢

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

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

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);

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>`

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.

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...

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2024-12-16 11:17:09

如果您将 XML 更改为

<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 样式解析。

以下是使用 NSXMLParser 格式化后的 XML 代码。

// 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;
}

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>

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文