Skip to content

Getting Started: 10. Exporting

Daniele Orlando edited this page Feb 21, 2016 · 3 revisions

The document can be exported in multiple ways.

DOMDocument:

$dom = $book->dom();

XML String:

Concise syntax

$xml =(string) $book;

Extended syntax

$xml = $book->xml();
<?xml version="1.0" encoding="UTF-8"?>
<book>
  ...
</book>

The XML declaration can be removed from the output string too.

$xml = $book->xml(true);
<book>
  ...
</book>

Not only the entire document but even only specific nodes (with their content) can be exported.

$xml = $book->query('//chapter')->xml();
<chapter lang="en" first="">Ideas About The Universe</chapter>
<chapter lang="en" last="">The Expanding Universe</chapter>

HTML String:

$html = $book->html();

The HTML export differs from the XML one for these aspects:

  • The HTML export has a DOCTYPE header, the XML export has an XML declaration;
  • The HTML export leaves not void tags — like div — open (<div></div>), the XML export use the void form for every empty tag.
<!DOCTYPE html>
<book>
  ...
</book>

The DOCTYPE declaration can be removed from the output string too.

$html = $book->html(true);
<book>
  ...
</book>

As for ->xml(), not only the entire document but even only specific nodes (with their content) can be exported.

$html = $book->query('//chapter')->html();
<chapter lang="en" first="">Ideas About The Universe</chapter>
<chapter lang="en" last="">The Expanding Universe</chapter>

File:

$book->save('book.xml');

As for ->xml(), the output document can be saved without the XML declaration

$book->save('book.xml', true);

or only specific nodes can be saved.

$book->query('//chapter')
     ->save('book_chapters.xml');

Saving is fluent too.

$book->save('book.xml')
     ->save('book_tidy.xml', true)
     ->query('//chapter')
     ->save('book_chapters.xml');

11. Importing 〉