PHPでXML作成
DOMで一個ずつ追加していくの面倒なので、arrayから作れないかなと思って調べてみると、XML_Serializerでできる様子。
<?php
require_once 'XML/Serializer.php';
$options = array(
"indent" => " ",
"linebreak" => "\n",
"typeHints" => false,
"addDecl" => true,
"encoding" => "UTF-8",
"rootName" => "feed",
"rootAttributes" => array(
"xmlns" => "http://www.w3.org/2005/Atom",
"xmlns:base" => "http://example.org/blog/entries",
"xml:lang" => "ja"),
"defaultTagName" => "entry",
"attributesArray" => "_attributes"
);
$serializer =& new XML_Serializer($options);
$entry = array(
"id" => "http://example.jp/entries",
"title" => "Blog Entries",
"updated" => date(DATE_ATOM),
"link" => array(
"_attributes" => array(
"rel" => "selt",
"type" => "application/atom+xml",
"href" => "http://example.jp/atom",
),
),
"link" => array(
"_attributes" => array(
"rel" => "alternate",
"type" => "text/html",
"href" => "http://example.jp/",
),
),
"entry" => array(
"title" => "Entry 1",
"author" => array(
"name" => "My name",
),
"updated" => date(DATE_ATOM),
"link" => array(
"_attributes" => array(
"rel" => "alternate",
"type" => "text/html",
"href" => "http://example.jp/entry/1",
),
),
"summary" => "summary",
),
);
$serializer->Serialize($entry);
echo $serializer->getSerializedData();
entryの中身は適当。
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="ja" xmlns="http://www.w3.org/2005/Atom" xmlns:base="http://examp
le.org/blog/entries">
<id>http://example.jp/entries</id>
<title>Blog Entries</title>
<updated>2007-09-01T15:56:34+09:00</updated>
<link href="http://example.jp/" rel="alternate" type="text/html" />
<entry>
<title>Entry 1</title>
<author>
<name>My name</name>
</author>
<updated>2007-09-01T15:56:34+09:00</updated>
<link href="http://example.jp/entry/1" rel="alternate" type="text/html"
/>
<summary>summary</summary>
</entry>
</feed>
カテゴリー: 日記