More Related Content
Similar to Xml builderの紹介
Similar to Xml builderの紹介 (20)
More from Hiraku Nakano (9)
Xml builderの紹介
- 2. @Hiraku (中野 拓)
某ポータルサイトのPHPプログラマ
PHP歴4年
好きなフレームワーク
Zend Framework
YAF
自己紹介
http://blog.tojiru.net/
https://github.com/hirak
- 4. RSS, Atom
WebAPI
GData
OData
KML
Gadget.xml
何とか.xmlたくさん
XMLと言えば
まだまだ現役
- 7. <?php $data = range(0,3) ?>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<?php foreach ($data as $v): ?>
<child><?php echo $v?></child>
<?php endforeach ?>
</root>
文字列連結
- 8. < “ > & → < " > &
エスケープを確実に
壊れたXMLになってしまうかも
タグの対応
インデントを保つのがつらい
文字列連結の問題
→おすすめしない
- 12. <?php
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$root = $doc->createElement('root');
$root->setAttribute('xmlns', 'http://example.com/');
$child = $doc->createElement('child');
$text = $doc->createTextNode('foo');
$child->appendChild($text);
$root->appendChild($child);
$doc->appendChild($root);
echo $doc->saveXML();
DOMのコード
- 17. XMLWriterのコード
<?php
$w = new XMLWriter;
$w->openURI('php://output');
$w->setIndent(true);
$w->setIndentString(' ');
$w->startDocument('1.0','UTF-8');
$w->startElement('root');
$w->writeAttribute('xmlns',
'http://example.com/');
$w->startElement('child');
$w->text('foo');
$w->endElement();
$w->endELement();
$w->endDocument();
- 26. classでバックエンドの切り替え
dom, xmlwriter, array
formatOutput
encoding, version
$json_builder = XML_Builder::factory(array(
'class' => 'array',
'serializer' => 'json_encode',
));
factory()のオプション
- 29. $hoge = 'Foo'
/* ... */
->$hoge($hoge)
/* ... */
->{$hoge . '_'}($hoge)
変数
←要素名もテキストも変数が
使える
←要素名に複雑な文字列を使う
- 39. <?php
require_once 'XML/Builder.php';
XML_Builder::factory(array('doctype' => XML_Builder::$HTML4_TRANSITIONAL))
->html(array('lang' => 'ja'))
->head
->meta_(array(
'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=UTF-8'
))
->meta_(array(
'http-equiv' => 'Content-Style-Type',
'content' => 'text/css'
))
->meta_(array(
'http-equiv' => 'Content-Script-Type',
'content' => 'text/javascript'
))
->meta_(array('name' => 'robots', 'content' => 'INDEX,FOLLOW'))
->title_('DOMで HTML かいたりしませんか')
->_
->body_
->_
->_echo;