Friday, September 28, 2012

Different kinds of file Parsing in iPhone


Different kinds of file Parsing in iPhone

1)HTML File Parsing

Example-1

Example-2

2)XML File Parsing

Example-1

Example-2

Knowledge of XPath


Following Tutorial Describes two common XML parsing mode (NSXMLParser & GDataXMLNode)

Commonly used analytical XML are two ways, they are based on a different API :
1.Tree-based API : The API approach is to XML as a tree structure, then the various parts of the tree as an object to handle, this is what we mean DOM (Document Object Model) approach. In the iPhone 's SDK contains a libxml2 framework (Framework) will be able to DOM parsing mode.Google 's GDataXML is also based on libxml2 , and therefore the use of GDataXML , you need to first import libxml2 .

2.Event-driven API: This is often used to resolve event-based, SAX parsing mode is representative of this analytical method. In the iPhone developed in this way can also be used to parse XML , but this is not Iphone SDK attributes ah, but Objective-C functions. In Objectvie-C species have special parsing XML class NSXMLParser .

NSXMLParser and CGataXML to use:

1. NSXMLParser analytical method
NSXMLParser parsing XML in the main work is relied on the agent (NSXMLParserDelegate) to achieve.

- ( void ) {viewDidLoad
[ Super viewDidLoad ];
//........
NSXMLParser * Parser = [[ NSXMLParser alloc ] initWithContentsOfURL : [ NSURL URLWithString : @ " http://lab.xxxxxx.com/xxx/xxx.xml " ]];
[Parser setDelegate : self ];
[Parser parse ];

for ( int i = 0 ; i <[ newsArray count ]; i + +) {
NSLog ( "%@" , [ newsArray objectAtIndex : i]);
}
}
To so few, they begin to resolve, and how to get XML data inside it?
Then go to agents to find it inside!
# Pragma mark NSMXLParser Delegate Methods
- ( void ) Parser: ( NSXMLParser *) Parser didStartElement: ( NSString *) elementName
namespaceURI: ( NSString *) namespaceURI QualifiedName: ( NSString *) qName attributes: ( NSDictionary *) attributeDict {
if ([elementName isEqualToString : @ "DocTitle" ]) {
if (! newsArray ) {
newsArray = [[ NSMutableArray alloc ] init ];
return ;
}
}
}

- ( void ) Parser: ( NSXMLParser *) Parser foundCharacters: ( NSString *) {String
if (! currentString ) {
currentString = [[ NSMutableString alloc ] init ];
}
[ currentString appendString : String];
[ currentString setString : [ currentString stringByTrimmingCharactersInSet : [ NSCharacterSet whitespaceAndNewlineCharacterSet ]]];
}

- ( void ) Parser: ( NSXMLParser *) Parser didEndElement: ( NSString *) elementName
namespaceURI: ( NSString *) namespaceURI QualifiedName: ( NSString *) qName {
if ([elementName isEqualToString : @ "DocTitle" ]) {
[ newsArray addObject : currentString ];
}
[ currentString Release ];
currentString = nil ;
}
Let me talk about the process to run, when the parser to initialize and run parse statement ([Parser parse ]), the program will jump to the first proxy agent approach which way to go. Method will be the first agent xml traverse again, and identify xml element name inside (elementName) , I'm looking at the information found (docTitle) , create an array to store the information, and of course not the first agent begin storing information.
The next will take a second agent, it will the first phase we are looking for agent information is stored in currentString in. Which I added a deal on access to the string statement [ currentString setString : [ currentString stringByTrimmingCharactersInSet : [ NSCharacterSet whitespaceAndNewlineCharacterSet ]]] , it can start the string with spaces and line breaks removed (as often encountered xml file format not standard).
Finally, the third proxy methods to access the information stored in our array.
Of course, this three-step program is running in the order of execution is not so strict, and the execution will be repeated, but generally the process is this.

1. DOM parsing method of GDataXMLNode
GDataXMLNode is based on libxml2 the third-party documents, more powerful features.
NSError * Error = nil ;
NSString * documentStr = [[ NSString alloc ] initWithContentsOfURL : [ NSURL URLWithString : @ "http://www.xxxx.com/x/xxx.xml" ] encoding : NSUTF8StringEncoding Error : & Error];
GDataXMLDocument * XMLDocument = [[ GDataXMLDocument alloc ] initWithXMLString : documentStr Options : 0 Error : & Error];
[DocumentStr Release ];
GDataXMLElement * rootElement = [XMLDocument rootElement ];
NSArray * newsArray = [rootElement elementsForName : @ "DocTitle" ];
for ( int i = 0 ; i <[newsArray count ]; i + +) {
NSLog ( "%@" , [[newsArray objectAtIndex : i] stringValue ]);
}
To so few, features, and above NSXMLParser , of course, is stored in the array GDataXMLElement objects rather than strings.
In the DOM parsing mode, the first XML file into an object or link, here is GDataXMLDocument object, and then each part of the object which is seen as an object, such as rootElement a GDataXMLElement object, which is the XML root of the tree , including the XML of all information. We directly rootElement look under the name: DocTitle the object, put it inside the array (because such objects may be more than one). We want to take docTitle object inside the contents of theHello iPhone , the object stringValue is.

No comments:

Post a Comment