return API version
string
apiVersion
()
create string representation of an attribute list
- require_once 'XML/Util.php';
-
- // build an attribute string
- $att = array(
- "foo" => "bar",
- "argh" => "tomato"
- );
-
- $attList = XML_Util::attributesToString($att);
string
attributesToString
(array $attributes, [boolean|array $sort = true], [boolean $multiline = false], [string $indent = ' '], [string $linebreak = "\n"], [integer $entities = XML_UTIL_ENTITIES_XML])
-
array
$attributes: attribute array
-
boolean|array
$sort: sort attribute list alphabetically, may also be an assoc array containing the keys 'sort', 'multiline', 'indent', 'linebreak' and 'entities'
-
boolean
$multiline: use linebreaks, if more than one attribute is given
-
string
$indent: string used for indentation of multiline attributes
-
string
$linebreak: string used for linebreaks of multiline attributes
-
integer
$entities: setting for entities in attribute values (one of XML_UTIL_ENTITIES_NONE, XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
Collapses empty tags.
string
collapseEmptyTags
(string $xml, [integer $mode = XML_UTIL_COLLAPSE_ALL])
-
string
$xml: XML
-
integer
$mode: Whether to collapse all empty tags (XML_UTIL_COLLAPSE_ALL) or only XHTML (XML_UTIL_COLLAPSE_XHTML_ONLY) ones.
create a CData section
- require_once 'XML/Util.php';
-
- // create a CData section
- $tag = XML_Util::createCDataSection("I am content.");
string
createCDataSection
(string $data)
-
string
$data: data of the CData section
create an XML comment
- require_once 'XML/Util.php';
-
- // create an XML start element:
- $tag = XML_Util::createComment("I am a comment");
string
createComment
(string $content)
-
string
$content: content of the comment
create an end element
- require_once 'XML/Util.php';
-
- // create an XML start element:
- $tag = XML_Util::createEndElement("myNs:myTag");
string
createEndElement
(string $qname)
-
string
$qname: qualified tagname (including namespace)
create a start element
- require_once 'XML/Util.php';
-
- // create an XML start element:
- $tag = XML_Util::createStartElement("myNs:myTag", array("foo" => "bar") ,"http://www.w3c.org/myNs#");
string
createStartElement
(string $qname, [array $attributes = array()], [string $namespaceUri = null], [boolean $multiline = false], [string $indent = '_auto'], [string $linebreak = "\n"])
-
string
$qname: qualified tagname (including namespace)
-
array
$attributes: array containg attributes
-
string
$namespaceUri: URI of the namespace
-
boolean
$multiline: whether to create a multiline tag where each attribute gets written to a single line
-
string
$indent: string used to indent attributes (_auto indents attributes so they start at the same column)
-
string
$linebreak: string used for linebreaks
create a tag
This method will call XML_Util::createTagFromArray(), which is more flexible.
- require_once 'XML/Util.php';
-
- // create an XML tag:
- $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], [string $namespaceUri = null], [integer $replaceEntities = XML_UTIL_REPLACE_ENTITIES], [boolean $multiline = false], [string $indent = "_auto"], [string $linebreak = "\n"])
-
string
$qname: qualified tagname (including namespace)
-
array
$attributes: array containg attributes
-
mixed
$content
-
string
$namespaceUri: URI of the namespace
-
integer
$replaceEntities: whether to replace XML special chars in content, embedd it in a CData section or none of both
-
boolean
$multiline: whether to create a multiline tag where each attribute gets written to a single line
-
string
$indent: string used to indent attributes (_auto indents attributes so they start at the same column)
-
string
$linebreak: string used for linebreaks
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)
)
- require_once 'XML/Util.php';
-
- $tag = array(
- "qname" => "foo:bar",
- "namespaceUri" => "http://foo.com",
- "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ),
- "content" => "I'm inside the tag",
- );
- // creating a tag with qualified name and namespaceUri
- $string = XML_Util::createTagFromArray($tag);
string
createTagFromArray
(array $tag, [integer $replaceEntities = XML_UTIL_REPLACE_ENTITIES], [boolean $multiline = false], [string $indent = "_auto"], [string $linebreak = "\n"])
-
array
$tag: tag definition
-
integer
$replaceEntities: whether to replace XML special chars in content, embedd it in a CData section or none of both
-
boolean
$multiline: whether to create a multiline tag where each attribute gets written to a single line
-
string
$indent: string used to indent attributes (_auto indents attributes so they start at the same column)
-
string
$linebreak: string used for linebreaks
build a document type declaration
- require_once 'XML/Util.php';
-
- // get a doctype declaration:
- $xmlDecl = XML_Util::getDocTypeDeclaration("rootTag","myDocType.dtd");
string
getDocTypeDeclaration
(string $root, [string $uri = null], [string $internalDtd = null])
-
string
$root: name of the root tag
-
string
$uri: uri of the doctype definition (or array with uri and public id)
-
string
$internalDtd: internal dtd entries
build an xml declaration
- require_once 'XML/Util.php';
-
- // get an XML declaration:
- $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)
check, whether string is valid XML name
XML names are used for tagname, attribute names and various other, lesser known entities.
An XML name may only consist of alphanumeric characters, dashes, undescores and periods, and has to start with a letter or an underscore.
- require_once 'XML/Util.php';
-
- // verify tag name
- $result = XML_Util::isValidName("invalidTag?");
- if (XML_Util::isError($result)) {
- print "Invalid XML name: " . $result->getMessage();
- }
mixed
isValidName
(string $string)
-
string
$string: string that should be checked
replacement for XML_Util::raiseError
Avoids the necessity to always require PEAR.php
object PEAR_Error
raiseError
(string $msg, integer $code)
-
string
$msg: error message
-
integer
$code: error code
replace XML entities
With the optional second parameter, you may select, which entities should be replaced.
- require_once 'XML/Util.php';
-
- // replace XML entites:
- $string = XML_Util::replaceEntities("This string contains < & >.");
string
replaceEntities
(string $string, [integer $replaceEntities = XML_UTIL_ENTITIES_XML])
-
string
$string: string where XML special chars should be replaced
-
integer
$replaceEntities: setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
reverse XML entities
With the optional second parameter, you may select, which entities should be reversed.
- require_once 'XML/Util.php';
-
- // reverse XML entites:
- $string = XML_Util::reverseEntities("This string contains < & >.");
string
reverseEntities
(string $string, [integer $replaceEntities = XML_UTIL_ENTITIES_XML])
-
string
$string: string where XML special chars should be replaced
-
integer
$replaceEntities: setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
split qualified name and return namespace and local part
- require_once 'XML/Util.php';
-
- // split qualified tag
- $parts = XML_Util::splitQualifiedName("xslt:stylesheet");
the returned array will contain two elements:
array(
"namespace" => "xslt",
"localPart" => "stylesheet"
);
array
splitQualifiedName
(string $qname, [string $defaultNs = null])
-
string
$qname: qualified tag name
-
string
$defaultNs: default namespace (optional)