PHPackages                             popphp/pop-dom - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. popphp/pop-dom

ActiveLibrary

popphp/pop-dom
==============

Pop DOM Component for Pop PHP Framework

4.0.7(6mo ago)419.3k↑112.5%4BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Jul 22Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-dom)[ Packagist](https://packagist.org/packages/popphp/pop-dom)[ Docs](https://github.com/popphp/pop-dom)[ RSS](/packages/popphp-pop-dom/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (30)Used By (4)

pop-dom
=======

[](#pop-dom)

[![Build Status](https://github.com/popphp/pop-dom/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-dom/actions)[![Coverage Status](https://camo.githubusercontent.com/ab0cd36832e17a73286732b5fe95e70b0054fc0dddb8065edc3ca15ee1aeee39/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d646f6d)](http://cc.popphp.org/pop-dom/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Parsing](#parsing)

Overview
--------

[](#overview)

`pop-dom` is a component for generating, rendering and parsing DOM documents and elements. With it, you can easily create or parse document nodes and their children and have control over node content and attributes.

`pop-dom` is a component of the [Pop PHP Framework](https://www.popphp.org/).

[Top](#pop-dom)

Install
-------

[](#install)

Install `pop-dom` using Composer.

```
composer require popphp/pop-dom

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-dom" : "^4.0.7"
}

```

[Top](#pop-dom)

Quickstart
----------

[](#quickstart)

### A simple DOM node fragment

[](#a-simple-dom-node-fragment)

```
use Pop\Dom\Child;

$div = new Child('div');
$h1  = new Child('h1', 'This is a header');
$p   = new Child('p');
$p->setNodeValue('This is a paragraph.');

$div->addChildren([$h1, $p]);

echo $div;
```

```

    This is a header
    This is a paragraph.

```

### Building a full DOM document

[](#building-a-full-dom-document)

```
use Pop\Dom\Document;
use Pop\Dom\Child;

// Title element
$title = new Child('title', 'This is the title');

// Meta tag
$meta = new Child('meta');
$meta->setAttributes([
    'http-equiv' => 'Content-Type',
    'content'    => 'text/html; charset=utf-8'
]);

// Head element
$head = new Child('head');
$head->addChildren([$title, $meta]);

// Some body elements
$h1 = new Child('h1', 'This is a header');
$p  = new Child('p', 'This is a paragraph.');

$div = new Child('div');
$div->setAttribute('id', 'content');
$div->addChildren([$h1, $p]);

// Body element
$body = new Child('body');
$body->addChild($div);

// Html element
$html = new Child('html');
$html->addChildren([$head, $body]);

// Create and render the DOM document with HTTP headers
$doc = new Document(Document::HTML, $html);
echo $doc;
```

```
>

        This is the title

            This is a header
            This is a paragraph.

```

[Top](#pop-dom)

Parsing
-------

[](#parsing)

You can parse from a string of XML or HTML and it will return an object of Child elements that you can further manipulate or edit to then output:

```
$html =
