Prepare an iPhone wap browser by yourself (iphone development-advanced)

Source: Internet
Author: User

Author: Sun Dongfeng 2009-12-01 (source)

 

In my previous article "BSD Socket for iPhone network communication, I tried to use BSD Socket on the iPhone platform to build a framework that is compatible with both TCP/IP and HTTP protocols. In the following articles, I will further improve the network communication engine and use it to write a simple wap browser.

 

The safari browser of the iPhone does not support WML parsing. Although I also believe that the old technology of WML will be eliminated sooner or later, however, WML is a "variant" of the XML structure.

 

Recently, the browser technology is very popular, and I am confused. Some time ago, Comrade Lei Jun of Kingsoft also invested in UCWeb. Although I do not think that browser technology has any profound technical content or the value of investment, in fact, the browser is a client at best, but since people are optimistic about it, the author's research is also not helpful, maybe after reading this article, readers can also take their own products to invest in Comrade Lei Jun :)

 

Let's get down to the truth with less gossip.

 

As mentioned above, WML is a "variant" or special case of the XML structure. Since it is a special case, it can be parsed as XML. The process of creating a browser is clear, as shown below:

 

2. encapsulate BSD Socket for HTTP requests.

2. parse the requested WML page into an XML data structure.

2. The WML tag (English name tag) to be displayed on the interface for rendering ).

2. display the rendered WML labels on the UI (UIView ).

 

The first article has been written in my previous article "BSD Socket for iPhone network communication". Of course, I will further improve it in the following article.

 

This article focuses on WML parsing. Because WML is a special case of XML data, parsing WML means parsing XML.

 

Speaking of XML parsing, the iPhone provides programmers with many tools, such as NSXMLParser. The interface definition of this class is as follows:

 

@ Interface NSXMLParser: NSObject {

@ Private

Void * _ parser;

Id _ delegate;

Id _ reserved1;

Id _ reserved2;

Id _ reserved3;

}

-(Id) initWithContentsOfURL :( NSURL *) url; // initializes the parser with the specified URL.

-(Id) initWithData :( NSData *) data; // create the parser from data

 

// Delegate management. The delegate is not retained.

-(Id) delegate;

-(Void) setDelegate :( id) delegate;

 

-(Void) setShouldProcessNamespaces :( BOOL) shouldProcessNamespaces;

-(Void) setShouldReportNamespacePrefixes :( BOOL) shouldReportNamespacePrefixes;

-(Void) setShouldResolveExternalEntities: (BOOL) shouldResolveExternalEntities;

 

-(BOOL) shouldProcessNamespaces;

-(BOOL) shouldReportNamespacePrefixes;

-(BOOL) shouldResolveExternalEntities;

 

-(BOOL) parse; // called to start the event-driven parse. Returns YES in the event of a successful parse, and NO in case of error.

-(Void) abortParsing; // called by the delegate to stop the parse. The delegate will get an error message sent to it.

 

-(NSError *) parserError; // can be called after a parse is over to determine parser state.

@ End

 

From the interface definition, we can see that this class parses XML using the SAX mode (Simple API for XML), while the SAX is event-driven, the basic workflow is to analyze the flow data of XML files. When a new element is found, a corresponding event is generated and corresponding user processing functions are called. On the iPhone, Apple uses the delegate mode. Every time a new element is found, the corresponding delegate interface is called to process XML tags.

 

Parsing XML in the SAX mode consumes less memory and is fast. However, you need to combine the parsed XML tags into a tree structure to make the program processing more complex.

 

For WML browsers, although there are not many tags, It is boring to fully support WML tags. Therefore, we use the Document Object Model (DOM) mode to parse XML files. The DOM mode analyzes the XML file stream at one time, and forms the corresponding tree structure in the memory, provides you with a series of interfaces to access and edit the tree structure. This method occupies a large amount of memory and is usually slower than the SAX mode. However, it is more convenient to provide an object-oriented access interface for programmers.

 

The full name of XML is the eXtensible Markup Language. The so-called "extensible" is because HTML and other languages are not extensible. labels in XML can be customized. For example, WML uses XML to customize a set of tags, so there is a wireless wap specification.

 

XML scalability refers to the scaling of the corresponding specifications and standards. First, the format must meet the basic requirements of XML. For example, the first line must have a declaration, and the nested layers of tags must be consistent. files that meet these requirements are even qualified XML files, it is called Well-formatted. Secondly, XML documents must meet the corresponding standards in terms of semantics because of their different content. These standards are defined by the corresponding "DTD file" or "Schema file, XML files that meet these definition requirements are called Valid.

 

I have used the open-source TinyXML parser in this article. This parser does not use the corresponding DTD file to verify the XML file, but it is very small and only contains two *. h file and four *. cpp file.

 

TinyXML is an open-source project. For more information, see http://www.grinninglizard.com/tinyxml/index.html.

 

After downloading the package, import the corresponding file to the project, for example:

 

Figure 1

The tinyxml. h file contains all declarations. You only need to include this file in the project.

Tinyxml. h defines many structures as follows:

 

Class TiXmlNode: public TiXmlBase

{

Friend class TiXmlDocument;

Friend class TiXmlElement;

...

}

 

These classes correspond to the Tree Structure in XML. The following XML document is taken as an example:

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<! -Example -->

<Food>

<Name> bread </name>

<Price unit = "$"> 1.5 </price>

<Description> made in China </description>

</Food>

 

The entire XML file is represented by TiXmlDocument. <food>, <name>, <price>, and <description> Each correspond to a TiXmlElement class, the first line of the XML document corresponds to the TiXmlDeclaration class, the second line corresponds to the TiXmlComment class, and the text "example" corresponds to the TiXmlText class. unit is a TiXmlAttribute attribute of the element price.

 

After importing the TinyXML package to the project, create an XMLParserEx. h file and an XMLParserEx. cpp file to encapsulate XML processing. The header file is defined as follows:

 

# Ifndef _ CC_XMLPARSEREX_H _

# Define _ CC_XMLPARSEREX_H _

# Include <stdio. h>

# Include "tinyxml. h"

 

# Define INVALID_ID-1

Class XMLParserEx

{

Public:

Static XMLParserEx * GetInstance ();

Static void Destroy ();

Void RemoveAll ();

Void parsexml (const char * buffer );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.