Source for file XMLDocument.php
Documentation is available at XMLDocument.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Services_Yahoo_Maps_XMLDocument class
* Copyright 2005 Bryan Dunlap
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* @package Services_Yahoo
* @author Bryan Dunlap <bdunlap@bryandunlap.com>
* @copyright 2005 Bryan Dunlap
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version CVS: $Id: XMLDocument.php,v 1.2 2006/10/02 12:53:33 mj Exp $
require_once "Services/Yahoo/Exception.php";
* Provides facilities for creating and modifying XML for use with the
* The Yahoo! Maps XML is based on geoRSS 2.0
* @package Services_Yahoo
* @author Bryan Dunlap <bdunlap@bryandunlap.com>
* @copyright 2005 Bryan Dunlap
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version Release: @package_version@
* Location of the GeoRSS 2.0 schema
const URI_GEO = "http://www.w3.org/2003/01/geo/wgs84_pos#";
* Location of the Yahoo! Maps schema
const URI_YMAPS = "http://api.maps.yahoo.com/Maps/V1/AnnotatedMaps.xsd";
* @param string $file (optional) a string containing the path to a valid
* @throws Services_Yahoo_Exception
$domDoc = DOMDocument ::load ($file);
* Adds a group sub-element to the groups element
* @param array $options an array containing valid attributes for the
* @throws Services_Yahoo_Exception
$validOptions = array ("id", "title",
if (!($groups = $this->domDoc->getElementsByTagName ("ymaps:Groups")
$groups = $this->domDoc->getElementsByTagName ("channel")
->appendChild ($this->domDoc->createElement ("ymaps:Groups"));
$group = $this->domDoc->createElement ("group");
foreach ($options as $name => $value) {
$group->appendChild ($this->setElementValue ($this->domDoc->createElement ($name),
$groups->appendChild ($group);
* Adds an item sub-element to the channel element
* @param array $options an array containing valid attributes for the
* @throws Services_Yahoo_Exception
$validOptions = array ("title", "link", "description",
"ymaps:Address", "ymaps:CityState",
"ymaps:Zip", "ymaps:Country",
"ymaps:PhoneNumber", "ymaps:Group",
"ymaps:BaseIcon", "ymaps:HoverIcon",
"ymaps:PopupIcon", "ymaps:ExtraLink",
"ymaps:ExtraImage", "ymaps:ItemUrl");
$channel = $this->domDoc->getElementsByTagName ("channel")
$item = $this->domDoc->createElement ("item");
foreach ($options as $name => $value) {
if ($name == "ymaps:ExtraLink") {
foreach ($value as $linkText => $href) {
$extraLink = $this->domDoc->createElement ("ymaps:ExtraLink",
$extraLink->setAttribute ("href", $href);
$item->appendChild ($extraLink);
} elseif ($name == "ymaps:ExtraImage") {
$extraImage = $this->domDoc->createElement ("ymaps:ExtraImage");
foreach ($value as $elementName => $elementValue) {
$extraImage->appendChild ($this->setElementValue ($this->domDoc->createElement ($elementName),
$item->appendChild ($extraImage);
$item->appendChild ($this->setElementValue ($this->domDoc->createElement ($name),
$channel->appendChild ($item);
* Sets the image sub-element of the channel element
* @param string $imageURL a string containing a valid URL to an image
$validOptions = array ("id", "title",
$image = $this->setChannelElement ("image", $imageUrl);
foreach ($options as $name => $value) {
* Sets the description sub-element of the channel element
* @param string $description a string containing the description
$this->setChannelElement ("description", $description);
* Sets the "defaultViewNumbered" attribute of the ymaps:Groups element
* @param boolean $defaultViewNumbered
$this->domDoc->getElementsByTagName ("ymaps:Groups")
->setAttribute ("defaultViewNumbered", (boolean) $defaultViewNumbered);
* Sets the ymaps::IntlCode sub-element of the channel element
* @param string $languageCode a string containing the language code
$this->setChannelElement ("ymaps:IntlCode", $languageCode);
* Sets the geo:lat sub-element of the channel element
* @param float $latitude a float representing latitude
$this->setChannelElement ("geo:lat", (float) $latitude);
* Sets the geo:long sub-element of the channel element
* @param float $longitude a float representing longitude
$this->setChannelElement ("geo:long", (float) $longitude);
* Sets the link sub-element of the channel element
* @param string $link a string containing a valid URL
$this->setChannelElement ("link", $link);
* Sets the title sub-element of the channel element
* @param string $title a string containing a title
$this->setChannelElement ("title", $title);
* Sets the ymaps:ZoomLevel sub-element of the channel element
* @param integer $zoomLevel an integer representing the map"s zoom level
$this->setChannelElement ("ymaps:ZoomLevel", $zoomLevel);
* Exports the DOM representation to XML and saves to file
* @param string $file the string containing the path of the
* @return boolean true if successful
* @throws Services_Yahoo_Exception
if (!$this->domDoc->save ($file)) {
* Exports the DOM representation to an XML string
* @throws Services_Yahoo_Exception
if (!($xmlString = $this->domDoc->saveXML ())) {
* Creates a new Yahoo! Maps XML Document
private function createNew ()
$domDoc = new DOMDocument ("1.0", "UTF-8");
$rss = $domDoc->appendChild ($domDoc->createElement ("rss"));
$rss->setAttribute ("version", "2.0");
$rss->setAttribute ("xmlns:geo", self ::URI_GEO );
$rss->setAttribute ("xmlns:ymaps", self ::URI_YMAPS );
$channel = $rss->appendChild ($domDoc->createElement ("channel"));
$channel->appendChild ($domDoc->createElement ("link"));
$channel->appendChild ($domDoc->createElement ("title"));
$channel->appendChild ($domDoc->createElement ("description"));
* Sets a channel sub-element
* @param string $name a string containing the
* @param string $value a string containing the
* @return object a DOMElement instance
private function setChannelElement ($name, $value)
if (!($element = $this->domDoc->getElementsByTagName ("channel")
->getElementsByTagName ($name)
$element = $this->domDoc->getElementsByTagName ("channel")
->appendChild ($this->domDoc->createElement ($name));
$element = $this->setElementValue ($element, $value);
* Sets an element's value
* @param object $element a DOMElement instance
* @param string $value a string containing the
* @return object a DOMElement instance
private function setElementValue ($element, $value)
$cData = array ("link", "url", "href", "image",
"ymaps:BaseIcon", "ymaps:HoverIcon",
"ymaps:PopupIcon", "ymaps:ExtraLink",
"ymaps:ExtraImage", "ymaps:ItemUrl");
(in_array($element->nodeName , $cData)) ?
$element->appendChild ($this->domDoc->createCDATASection ($value)) :
$element->nodeValue = $value;
Documentation generated on Fri, 20 Apr 2007 14:30:10 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|