您的位置:首页 > 理论基础 > 计算机网络

iphone--XML以及网络上的图片显示

2011-09-23 17:53 267 查看
xml处理:

iPhone开发中,XML处理的方式与.net区别很大。个人感觉没有donot那么方便。苹果sdk中提供了NSXMLParser这个类库来解析xml。它需要一个url作为输入参数,通过NSXMLParser的委托方法来解析这个xml文件。NSXMLParser中主要有三个委托方法来解析XML:

1、didStartElement

2、didEndElement

3、foundCharacters

didStartElement:当解析器对象遇到xml的开始标记时,调用这个方法。

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

当解析器对象遇到xml的结束标记时,调用这个方法。

-(
void
)parser:(
NSXMLParser
*)parser
didEndElement:(
NSString
*)elementName
namespaceURI:(
NSString
*)namespaceURI
qualifiedName:(
NSString
*)qName
foundCharacters:
当解析器找到开始标记和结束标记之间的字符时,调用这个方法。

-
(
void
)parser:(
NSXMLParser
*)parser
foundCharacters:(
NSString
*)string
在iphone中显示网络上的图片:

通过一个url展示图片,分为三个步骤:

1、通过url创建一个NSURL对象

2、通过NSURL对象将图片数据加载到NSData对象中

3、将NSData对象赋给UIImage

NSURL
*url
=[
NSURL
URLWithString:@”www.cnblogs.com\images\sample.jpg”];
NSData
*data
=[
NSData
dataWithContentsOfURL:url];
UIImage
*image=[[UIImagealloc]intiWithData:data];
下面我们将图片的地址保存在服务器的一个xml文件中,通过解析xml,将图片的地址解析出来,然后展示。

创建一个viewbasedapplication,命名为XMLWebImages

打开XMLWebImagesViewController.h。添加一个名为webScrollView的UIScrollView类型的对象属性,并加上IBOutlet标识。

#import
<UIKit/UIKit.h>@interfaceXMLWebImageViewController:UIViewController
{
IBOutlet
UIScrollView
*webScrollView;
}
@property
(
nonatomic
,
retain)
IBOutlet
UIScrollView
*webScrollView;
@synthesize
scrollView;
创建一个命名为XMLWebView,并继承UIView的子类,这这个子类中添加UIImageView属性对象,命名为imageView,并加上IBOutlet标识。

@interface
XMLWebView
:UIView
{
IBOutlet
UIImageView
*imageView;
}
@property
(
nonatomic
,
retain)
IBOutlet
UIImageView
*imageView;
@synthesize
imageView;
创建一个.XIB文件,上面放置一个UIImageView,与上面的代码中的imageView建立关联。

创建一个实体类,这个类里面只有一个属性,用来存放图片的地址:

@interface
XMLWebElement:
NSObject
{
UIImage
*imgXML;
}
@property
(
nonatomic
,
retain)UIImage*imgXML;
@end
@synthesize
image;
现在创建从服务器上获取xml,解析xml,展示图片的代码。打开XMLWebImagesViewController.h,创建一个NSXMLParser对象。

一个存放链接实体对象的数组xmlElements。一个临时的XMLWebElement对象。

@interface
XMLWebImagesViewController:
UIViewController
{
IBOutlet
UIScrollView
*scrollview;
NSXMLParser
*xmlParser;
NSMutableString
*currentNode;
NSMutableArray
*xmlElements;
XMLWebElement
*tempElement;
}
在viewDidLoad方法中为xmlElements分配内存,初始化parser对象的xml地址:

-
(
void
)viewDidLoad
{
[
super
viewDidLoad];
xmlElements
=[[
NSMutableArray
alloc]
init];
xmlParser
=[[
NSXMLParser
alloc]
initWithContentsOfURL:
[
NSURL
URLWithString:@
"http://www.cnblogs.com/images.xml"
]];
[xmlParser
setDelegate:
self
];
[xmlParser
parse];
}
服务器上的xml结构如下:解析xml的三个方法的代码如下:

<WebImages>
<image>
<URL>http://www.cnblogs.com/testimage1.jpg</URL>
</image>
<images>
<URL>http://www.cnblogs.com/testimage2.jpg</URL>
</images>
</WebImages>


解析xml的三个委托方法代码如下:

-
(
void
)xmlParser:(
NSXMLParser
*)xmlParser
didStartElement:
(
NSString
*)elementName
namespaceURI:(
NSString
*)namespaceURI
qualifiedName:(
NSString
*)qName
attributes:(
NSDictionary
*)attributeDict
{
if
(![elementName
compare:@
"PictureInfo"
])
{
tempElement
=[[iCodeBlogXMLElementalloc]init];
}
else
if
(![elementName
compare:@
"imageURL"
])
{
currentAttribute
=[
NSMutableString
string];
}
else
if
(![elementName
compare:@
"imageTitle"
])
{
currentAttribute
=[
NSMutableString
string];
}
}
-
(
void
)parser:(
NSXMLParser
*)parser
didEndElement:(
NSString
*)
elementName
namespaceURI:(
NSString
*)namespaceURI
qualifiedName:(
NSString
*)qName
{
if
(![elementName
compare:@
"PictureInfo"
])
{
[xmlElementObjects
addObject:tempElement];
}
else
if
(![elementName
compare:@
"imageURL"
])
{
NSURL
*imageURL
=[
NSURL
URLWithString:currentAttribute];
NSData
*data
=[
NSData
dataWithContentsOfURL:imageURL];
UIImage
*image=[[UIImagealloc]initWithData:data];
[tempElement
setImage:image];
}
else
if
(![elementName
compare:@
"imageTitle"
])
{
NSLog
(@
"The
imagetitleis%@"
,
currentAttribute);
[tempElement
setImageTitle:currentAttribute];
}
else
if
(![elementName
compare:@
"Pictures"
])
{
[
self
layoutSubview];
}
}
-
(
void
)parser:(
NSXMLParser
*)parser
foundCharacters:(
NSString
*)string
{
if
(
self
.currentAttribute)
{
[
self
.currentAttribute
appendString:string];
}
}
这样我们就将xml中的图片地址解析出来。然后我们下面代码展示图片:

NSURL
*url
=[
NSURL
URLWithString:urlString];
NSData
*data
=[
NSData
dataWithContentsOfURL:url];
UIImage
*image=[[UIImagealloc]intiWithData:data];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: