Class XML_Util

Description

utility class for working with XML documents

  • todo: method to get doctype declaration
  • author: Stephan Schmidt <mailto:schst@php.net>
  • version: 0.1

Located in Program_Root/XML_Util.php (line 48)

PEAR
   |
   --XML_Util
Method Summary
 string apiVersion ()
 string attributesToString (array $attributes, [boolean $sort = true])
 string createTag (string $qname, [array $attributes = array()], [mixed $content = null], [mixed $namespaceUri = null], [boolean $replaceEntities = true])
 string createTagFromArray (array $tag, [boolean $replaceEntities = true])
 string getXMLDeclaration ([string $version = "1.0"], [string $encoding = null], [mixed $standalone = null], boolean $standAlone)
 mixed isValidName (string $string)
 string replaceEntities (string $string)
 array splitQualifiedName (string $qname)
Methods
apiVersion (line 57)

return API version

  • return: API version
  • static:
  • access: public
string apiVersion ()
attributesToString (line 148)

create string representation of an attribute list


1 require_once 'XML/Util.php';
2
3 // build an attribute string
4 $att = array(
5 "foo" => "bar",
6 "argh" => "tomato"
7 );
8
9 $attList = XML_Util::attributesToString($att);

string attributesToString (array $attributes, [boolean $sort = true])
  • array $attributes: attribute array
  • boolean $sort: sort attribute list alphabetically
createTag (line 185)

create a tag

This method will call XML_Util::createTagFromArray(), which is more flexible.


1 require_once 'XML/Util.php';
2
3 // create an XML tag:
4 $tag = XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#");

string createTag (string $qname, [array $attributes = array()], [mixed $content = null], [mixed $namespaceUri = null], [boolean $replaceEntities = true])
  • string $qname: qualified tagname (including namespace)
  • array $attributes: array containg attributes
  • mixed $content
  • boolean $replaceEntities: whether to replace XML special chars in content or not
createTagFromArray (line 241)

create a tag from an array

this method awaits an array in the following format

 array(
  "qname"        => $qname         // qualified name of the tag
  "namespace"    => $namespace     // namespace prefix (optional, if qname is specified or no namespace)
  "localpart"    => $localpart,    // local part of the tagname (optional, if qname is specified)
  "attributes"   => array(),       // array containing all attributes (optional)
  "content"      => $content,      // tag content (optional)
  "namespaceUri" => $namespaceUri  // namespaceUri for the given namespace (optional)
   )
 


1 require_once 'XML/Util.php';
2
3 $tag = array(
4 "qname" => "foo:bar",
5 "namespaceUri" => "http://foo.com",
6 "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ),
7 "content" => "I'm inside the tag"
8 );
9 // creating a tag with qualified name and namespaceUri
10 $string = XML_Util::createTagFromArray($tag);

string createTagFromArray (array $tag, [boolean $replaceEntities = true])
  • array $tag: tag definition
  • boolean $replaceEntities: whether to replace XML special chars in content or not
getXMLDeclaration (line 109)

build an xml declaration


1 require_once 'XML/Util.php';
2
3 // get an XML declaration:
4 $xmlDecl = XML_Util::getXMLDeclaration("1.0", "UTF-8", true

string getXMLDeclaration ([string $version = "1.0"], [string $encoding = null], [mixed $standalone = null], boolean $standAlone)
  • string $version: xml version
  • string $encoding: character encoding
  • boolean $standAlone: document is standalone (or not)
isValidName (line 353)

check, whether string is valid XML name

<p>XML names are used for tagname, attribute names and various other, lesser known entities.</p> <p>An XML name may only consist of alphanumeric characters, dashes, undescores and periods, and has to start with a letter or an underscore. </p>


1 require_once 'XML/Util.php';
2
3 // verify tag name
4 $result = XML_Util::isValidName("invalidTag?");
5 if (XML_Util::isError($result)) {
6 print "Invalid XML name: " . $result->getMessage();
7 }

  • return: true, if string is a valid XML name, PEAR error otherwise
  • todo: support for other charsets
  • static:
  • access: public
mixed isValidName (string $string)
  • string $string: string that should be checked
replaceEntities (line 81)

replace XML entities

chars that have to be replaced are '&' and '<', furthermore '>', ''' and '"' have entities that can be used.


1 require_once 'XML/Util.php';
2
3 // replace XML entites:
4 $string = XML_Util::replaceEntities("This string contains < & >.");

  • return: string with replaced chars
  • usedby: XML_Util::attributesToString() - to replace XML entities in attribute values
  • todo: optional parameter to supply additional entities
  • static:
  • access: public
string replaceEntities (string $string)
  • string $string: string where XML special chars should be replaced
splitQualifiedName (line 312)

split qualified name and return namespace and local part


1 require_once 'XML/Util.php';
2
3 // split qualified tag
4 $parts = XML_Util::splitQualifiedName("xslt:stylesheet");
the returned array will contain two elements:
 array(
       "namespace" => "xslt",
       "localPart" => "stylesheet"
      );
 

  • return: array containing namespace and local part
  • usedby: XML_Util::createTagFromArray() - to get local part and namespace of a qualified name
  • static:
  • access: public
array splitQualifiedName (string $qname)
  • string $qname: qualified tag name

Documention generated on Fri, 1 Aug 2003 17:12:19 +0200 by phpDocumentor 1.2.1