Home > PHP > PHPでXML作成

PHPでXML作成

9月 1st, 2007
Goto comments Leave a comment

DOMで一個ずつ追加していくの面倒なので、arrayから作れないかなと思って調べてみると、XML_Serializerでできる様子。

 "    ",
  "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の中身は適当。



    http://example.jp/entries

    2007-09-01T15:56:34+09:00

    

        
            My name
        
        2007-09-01T15:56:34+09:00

        summary
    

cockok PHP

  1. No comments yet.
  1. No trackbacks yet.