Skip to content

Latest commit

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

XML Builder for PHP 7

A library for creating XML documents written in PHP 7.

Build StatusCoverage

Installation with Composer

This package is available on packagist, so you can use Composer to install it: composer require lib16/xml

Basic Usage

<?phprequire_once'vendor/autoload.php';
useLib16\XML\Xml;
class Kml extends Xml
{
constMIME_TYPE = 'application/vnd.google-earth.kml+xml';
constFILENAME_EXTENSION = 'kml';
constXML_NAMESPACE = 'http://www.opengis.net/kml/2.2';
publicstaticfunctioncreateKml(): self
{
returnstatic::createRoot('kml');
}
publicfunctionplacemark(
string$name,
string$description,
float$longitude,
float$latitude,
float$altitude = null
): self {
$pm = $this->append('Placemark');
$pm->append('name', $name);
$pm->append('description', $description);
$pm->append('Point')->append(
'coordinates',
implode(',', array_filter([
$longitude,
$latitude,
$altitude
]))
);
return$pm;
}
}
$myKml = Kml::createKml();
$myKml->placemark(
'Cologne Cathedral',
'Cologne Cathedral is a Roman Catholic cathedral in Cologne, Germany.',
50.9413,
6.958
);
//$myKml->headerfields('cologne-cathedral');print$myKml;

The generated markup:

<?xml version="1.0" encoding="UTF-8" ?>
<kmlxmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Cologne Cathedral</name>
<description>Cologne Cathedral is a Roman Catholic cathedral in Cologne, Germany.</description>
<Point>
<coordinates>50.9413,6.958</coordinates>
</Point>
</Placemark>
</kml>

The Adhoc trait

Adhoc allows you to use any method name not previously defined to add XML elements or attributes.

<?phprequire_once'vendor/autoload.php';
useLib16\XML\{Xml, Adhoc};
class Html extends Xml
{
use Adhoc;
constXML_DECLARATION = false;
constDOCTYPE = '<!DOCTYPE html>';
constHTML_MODE_ENABLED = true;
publicstaticfunctioncreateHtml(
string$lang = null,
string$manifest = null
): self {
returnstatic::createRoot('html')
->attrib('lang', $lang)
->setManifest($manifest);
}
}
$html = Html::createHtml('en');
$body = $html->body();
$article = $body->article();
$article->h1('Scripting languages');
$article->p(
Html::abbr('PHP')->setTitle('PHP: Hypertext Preprocessor')
. ' is a server-side scripting language designed for web development but also used as a general-purpose programming language.'
);
print$html;

The generated markup:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html><htmllang="en"><body><article><h1>Scripting languages</h1><p><abbrtitle="PHP: Hypertext Preprocessor">PHP</abbr> is a
server-side scripting language designed for web development
but also used as a general-purpose programming language.</p></article></body></html>

About

lib16 XML Builder is a PHP 7 library for creating XML documents.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages