[Zend PHP5 Cerification] Lectures -- 4. XML & Web Service

XML

 Well Formed

– Single rootlevel tag

– Tags must beopened and closed properly

– Entities mustbe well formed

 Valid

– Well formed

– Contain a DTD

– Follow thatDTD

ParsingXML

SAX

Simple API forXML

Event based approach

whereevery ‘event’ encountered must be coded for and handled. Eventsinclude opentag, close tag, tag data

DOM

Document ObjectModel

Loads entire documentinto ram, then creates an internal tree representation.

 

SimpleXML

Uses DOM method

Very easy to use

Memory Intensive

http://ca.php.net/simplexml

 

SimpleXML
Parsing XML Documents
Theonly restraint is that the XML document must be well-formed, orSimpleXML will emit warnings and fail to parse it. Also, while theW3C has published a recommended specification for XML1.1, SimpleXMLsupports only version 1.0 documents. Again, SimpleXML will emit awarning and fail to parse the document if it encounters an XMLdocument with a version of 1.1.

All objects created bySimpleXML are instances of the SimpleXMLElement class. Thus, whenparsing a document or XML string, you will need to create a newSimpleXMLElement
objectsimplexml_load_string ( string $data [, string $class_name [, int$options [, string $ns [, bool $is_prefix]]]] )
Interpretsa string of XML into an object
Takes a well-formed XML string andreturns it as an object. pairing file_get_contents() withsimplexml_load_string()

however, in a real-world scenario, itwould make much more sense to simply usesimple_xml_load_file():
objectsimplexml_load_file ( string $filename [, string $class_name [, int$options [, string $ns [, bool $is_prefix]]]] )

SimpleXMLElementclass
// Load an XMLfile
$library = new SimpleXMLElement(’library.xml’, NULL,true);
Note here that the second method also passes two additionalarguments to SimpleXMLElement’s constructor. The second argumentoptionally allows the ability to specify additional libxml parametersthat influence the way the library parses the XML. It is notnecessary to set any of these parameters at this point, so we left itto NULL. The third parameter is important, though, because it informsthe constructor that the first argument represents the path to afile, rather than a string that contains the XML dataitself.

SimpleXMLElement::xpath()
SimpleXMLElement::addChild()
SimpleXMLElement::addAttribute()

asXML()method of $library SimpleXMLElement.
Called without a parameter,the asXML() method returns an XML string. However asXML() alsoaccepts a file path as a parameter, which will cause it to save theXML document to the given path and return a Boolean value to indicatethe operation’s success.

If a file with the same pathalready exists, a call to asXML() will overwrite it without warning(provided that the user account under which PHP is running has theproper permissions).

It is possible to remove child elements,though, using the following method.
$library->book[0] =NULL;

This only removes child elements and their attributes,however. It will not remove attributes from the element at the booklevel. Thus, the isbn attribute remains. You may set this attributeto NULL, but doing will only cause it to become empty and will notactually remove it. To effectively remove children and attributes,you must export your SimpleXMLElement to DOM, where this morepowerful functionality is possible.

xpathand Namespaces
Xpath: Combinesthe mojo of Regex with some SQL likeresults
http://www.w3schools.com/xpath/xpath_syntax.asp
XMLNamespaces provide a method to avoid element name conflicts.
Whena namespace is defined in the start tag of an element, all childelements with the same prefix are associated with the samenamespace.
...
<h:tablexmlns:h="
http://www.w3.org/TR/html4/">
...
Notethat the address used to identify the namespace is not used by theparser to look up information. The only purpose is to give thenamespace a unique name. However, very often companies use thenamespace as a pointer to a real Web page containing informationabout the namespace. Try to go to
http://www.w3.org/TR/html4/.
DefaultNamespaces
Defining a defaultnamespace for an element saves us from using prefixes in all thechild elements.

<tablexmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
Whenyou start using XSL, you will soon see namespaces in real use. XSLstyle sheets are used to
transform XML documents into otherformats, like HTML.
If you take a close look at the XSL documentbelow, you will see that most of the tags are HTML tags.
The tagsthat are not HTML tags have the prefix xsl, identified by the
namespace "
http://www.w3.org/1999/XSL/Transform":

 

<?xmlversion="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheetversion="1.0"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">

<xsl:templatematch="/">
<html>
<body>
<h2>MyCD Collection</h2>
<table border="1">
<tr>
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>


XSL
Itstarted with XSL and ended up with XSLT, XPath, and XSL-FO.


DOM
DomDocument
   class

$dom = newDomDocument();
$dom->load("library.xml");

stringversion:
$dom = newDomDocument();
$dom->loadXML($xml);

DomDocument::loadHtmlFile()  and DomDocument::loadHTML()

DomDocument::save()  (to afile) ,  DomDocument::saveXML() (to a string) ,DomDocument::saveHTML() (also to a string, but saves an HTML documentinstead of an XML file), and DomDocument:saveHTMLFile() (to a file inHTML format).

XPathQueries
One of the most powerfulparts of the DOM extension, is its integration with XPath—in fact,Dom Xpath is far more powerful than the SimpleXML equivalent. 

 $dom= new DomDocument();
$dom->load("library.xml");
$xpath= new DomXPath($dom);
$xpath->registerNamespace("lib","
http://example.org/library");
$results= $xpath->query("//lib:title/text()");
foreach($results as $book) {
echo $book->data;
}

A call toDomXpath::query() will return a DomNodeList object; you can find outhow many items it contains by using the length property, and thenaccess any one of them with the item() method. You can also iteratethrough the entire collection using a foreach()loop.

DomNode
DomDocument::createElement(),
DomDocument::createElementNS(),and DomDocument::createTextNode() methods

$dom = newDomDocument();
$dom->load("library.xml");
$book =$dom->createElement("book");
$book->setAttribute("meta:isbn","0973589825");
$title =$dom->createElement("title");
$text =$dom->createTextNode("php|architect’s Guide to PHP DesignPatterns");
$title->appendChild($text);
$book->appendChild($title);
$author= $dom->createElement("author","Jason E.Sweat");
$book->appendChild($author);
$publisher =$dom->createElement("pub:publisher", "Marco Tabini&amp; Associates,Inc.");
$book->appendChild($publisher);
$dom->documentElement->appendChild($book);

DomNode::appendChild()and DomNode::insertBefore()
DomNode::appendChild()and DomNode::insertBefore() will move the node to the new location.If you wish to duplicate a node, use “DomNode::cloneNode()”first.

Modifying Data

$xml= <<<XML
<xml>
<text>some texthere</text>
</xml>
XML;
$dom = newDOMDocument();
$dom->loadXML($xml);
$xpath = newDomXpath($dom);
$node =$xpath->query("//text/text()")->item(0);
$node->data= ucwords($node->data);
echo $dom->saveXML();

RemovingData
There are three types ofdata you may want to remove from an XML document: attributes,elements and CDATA. DOM provides a different method for each of thesetasks: DomNode::removeAttribute(), DomNode::removeChild() andDomCharacterData::deleteData(). 

WorkingWithNamespaces
DOM is more thancapable to handle namespaces on its own and, typically, you can, forthe most part, ignore them and pass attribute and element names withthe appropriate prefix directly to most DOM functions.

$dom =new DomDocument();
$node =$dom->createElement(’ns1:somenode’);
$node->setAttribute(’ns2:someattribute’,’somevalue’);
$node2 =$dom->createElement(’ns3:anothernode’);
$node->appendChild($node2);
//Set xmlns:* attributes
$node->setAttribute(’xmlns:ns1’,’http://example.org/ns1’);
$node->setAttribute(’xmlns:ns2’,’http://example.org/ns2’);
$node->setAttribute(’xmlns:ns3’,’http://example.org/ns3’);
$dom->appendChild($node);
echo$dom->saveXML();

We can try to simplify the use ofnamespaces somewhat by using the DomDocument::createElementNS()and DomNode::setAttributeNS() methods

$dom = newDomDocument();
$node =$dom->createElementNS(’http://example.org/ns1’,’ns1:somenode’);
$node->setAttributeNS(’http://example.org/ns2’,’ns2:someattribute’, ’somevalue’);
$node2 =$dom->createElementNS(’http://example.org/ns3’,’ns3:anothernode’);
$node3 =$dom->createElementNS(’http://example.org/ns1’,’ns1:someothernode’);
$node->appendChild($node2);
$node->appendChild($node3);
$dom->appendChild($node);
echo$dom->saveXML();

Interfacingwith SimpleXML
You can importSimpleXML objects for use with DOM by usingdom_import_simplexml():

$sxml =simplexml_load_file(’library.xml’);
$node =dom_import_simplexml($sxml);
$dom = newDomDocument();
$dom->importNode($node,true);
$dom->appendChild($node);

The opposite is alsopossible, by using the aptly-named simplexml_import_dom()function:

$dom = newDOMDocument();
$dom->load(’library.xml’);
$sxe =simplexml_import_dom($dom);
echo $sxe->book[0]->title;


SAXand DOM
SAX
–Simple API for XML
. Event based approach where every ‘event’encountered must be coded for and handled
. Events include opentag, close tag, tag data

DOM
–Document Object Model
.Loads entire document into ram, thencreates an internal tree representation

WebServices  XML-RPC SOAP REST
PHP5 contains tools particularly suited for SOAP and REST Webservices

SOAP  requestsare sent using post.
SOAP isintrinsically tied to XML because all messages sent to and from aSOAP server are sent in a SOAP envelope that is an XML wrapper fordata read and generated by the SOAP server. Creating the XML for thiswrapper can be a tedious process and, therefore, many tools andexternal PHP libraries have been created to aid developers in thecumbersome process of forming SOAP requests and reading SOAP serverresponses. PHP 5 simplifies this process with its SOAPextension—which makes the creation of both servers and clients veryeasy.

WSDL document
This, in turn, is yet another XMLdocument that describes the function calls made available by a Webservice, as well as any specialized data types needed by it.

TheSoapClient class
It isessentially a one-stop solution to creating a SOAP client—all youreally need to do is provide it with the path to a WSDL file, and itwill automatically build a PHP-friendly interface that you can calldirectly from your scripts.  

e.g.

try
{
$client= new SoapClient(’http://api.google.com/GoogleSearch.wsdl’);
$results= $client->doGoogleSearch($key, $query, 0, 10, FALSE, ’’,FALSE, ’’, ’’, ’’);
foreach ($results->resultElementsas $result)
{
echo ’<a href="’ .htmlentities($result->URL) . ’">’;
echohtmlentities($result->title, ENT_COMPAT, ’UTF-8’);
echo’</a><br/>’;
}
}
catch (SoapFault $e)
{
echo$e->getMessage();
}

$key: Google developer Key.
$query:The search Keyword phrase.

SoapClient uses the WSDL file toconstruct an object mapped to the methods defined by the web service;thus, $client will now provide the methods doGetCachedPage(),doSpellingSuggestion(), and doGoogleSearch().

The constructorof the SOAPClient class also accepts, as an optional secondparameter, an array of options that can alter its behaviour; forexample, you can change the way data is encoded, or whether theentire SOAP exchange is to be compressed, and so on. If you areaccessing a SOAP service that does not have a WSDL file, it ispossible to create a SOAP client in non-WSDL mode by passing a NULLvalue to the SoapClient constructor instead of the location of theWSDL file. In this case, you will have to pass the URI to the Webservice’s entry point as part of the secondparameter.

Debugging
$client = newSoapClient(’http://api.google.com/GoogleSearch.wsdl’,array(’trace’ => 1));

SOAPServer
Requests are encapsulated within a SOAP Envelope
Responsesare XML documents with similar Envelope & Body sections
Whencreating a SOAP server, you simply start with a class that containsthe methods you wish to make available to the public through a Webservice and use it as the basis for a SoapServer instance.

classMySoapServer
{
public function getMessage()
{
return’Hello, World!’;
}
public function addNumbers($num1,$num2)
{
return $num1 + $num2;
}
}

When creating aSOAP server with SoapServer, you must decide whether your server willoperate in WSDL or non-WSDL mode. At present, SoapServer will notautomatically generate a WSDL file based on an existing PHP class,although this feature is planned for a future release. For now, youcan either create your WSDL files manually—usually an incrediblytedious task, use a tool (like the Zend Studio IDE) that willgenerate one for you, or choose not to provide one at all.

$options= array(’uri’ => ’http://example.org/soap/server/’);
$server= new SoapServer(NULL,$options);
$server->setClass(’MySoapServer’);
$server->handle();

Whilethis SOAP service will work just fine in non-WSDL mode, it isimportant to note that aWSDL file can be helpful to both users of theservice and to the SoapServer object itself. For users, a WSDL filehelps expose the various methods and data types available. For theserver, the WSDL file allows the mapping of different WSDL types toPHP classes, thus making handling complex data simpler.

$options= array(
’location’ =>’http://example.org/soap/server/server.php’,
’uri’ =>’http://example.org/soap/server/’
);
$client = newSoapClient(NULL, $options);
echo $client->getMessage() ."\n";
echo $client->addNumbers(3, 5) . "\n";

REST: 
Requests look like filled out forms, can use either GET orPOST
Response is an XML document
Request and Response isdefined by the service provider


RepresentationalState Transfer, the focus is on the presence of resources in thesystem.
Services that use the REST architecture are referred to asRESTful services; those who use or provide RESTful services aresometimes humorously referred to as RESTafarians.
There are anumber of RESTful Web services, the most popular of which thrive inthe blogosphere. In a loose sense,Web sites that provide RSS and RDF(Resource Description File) feeds provide a RESTful service.
MostRESTful services respond with XML data, and SimpleXML provides thebest interface to interact with them.
The popular bookmarkingsite,
del.icio.us,is one example of a Web site providing a REST service that returnsXML data ready for SimpleXML to consume.Note that del.icio.ususes HTTP authentication over SSL for its REST URIs; most RESTfulservices provide some kind of authentication or developer key schemeto gain access to the service.

posted @ 2010-06-29 17:25  DavidHHuan  阅读(526)  评论(0编辑  收藏  举报