Apr 1

逆引きrhaco64: SimpleTagを用いてxmlを生成する

rhacoではSimpleTagを使って動的にxmlを生成することが出来ます。

$tag = new SimpleTag("parent",array(
  new SimpleTag("child","child1"),
  new SimpleTag("child","child2"),
  new SimpleTag("child","child3"),
  ),
  array("param","param1")
);


とすると
<parent param="param1">
<child>child1</child>
<child>child2</child>
<child>child3</child>
</parent>


というタグを作れます。
後は、文字列として取得したい場合は
$str = $tag->get();//falseでもOK。上のタグそのものの文字列が得られる
$str = $tag->get(true);//xmlタグを追加した文字列が得られます


xmlとして標準出力に出力したい場合は
$tag->output();

を用います。
rhacoではxmlを良く扱うため、このような便利なクラスが用意されています。色々使ってみると面白いですよ。

| comment(0)