カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

XMLをPHP1行で解析

foreach(simplexml_load_file("ファイルのパス")->item as $it) echo $it->description
1,037 views

PHPでXMLファイルの解析

まずはXMLのサンプル

<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF
  xmlns="http://purl.org/rss/1.0/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:admin="http://webns.net/mvcb/"
  xml:lang="ja">
  <channel rdf:about="http://www.example.jp/test.rdf">
    <title>title</title>
    <link>http://www.example.jp/</link>
    <dc:date>2006-07-06T20:14:34+09:00</dc:date>
    <description>description</description>
    <admin:generatorAgent rdf:resource="http://www.infomaker.jp/editorlite/?v=0.92b" />
    <items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.example.jp/#he20060706"/>
      </rdf:Seq>
    </items>
  </channel>

  <item rdf:about="http://www.example.jp/#he20060706">
    <title>test</title>
    <link>http://www.example.jp/</link>
    <dc:date>2006-07-06</dc:date>
    <description>description</description>
  </item>

</rdf:RDF>

PHPで解析します。

// XMLファイルを読み込みインスタンスを生成
$xml = simplexml_load_file("ファイルのパス");
//$xml = new SimpleXMLElement("ファイルのパス", null, true); // この書き方でも可

// channel要素がある場合
//$channel = $xml->channel;
// 下のforeach取り出し元配列を$channel->itemに変更

foreach($xml->item as $item) {

    // 各内容を変数に格納
    //{
        $url         = $item->link;
        $title       = $item->title;
        $description = $item->description;

        // Namespace付の子要素を取得
        //{
            // childrenメソッドにて取得(引数のURLはxmlns:dc=[URL]から取得)
            $dc = $item->children('http://purl.org/dc/elements/1.1/');

            $date = date("Y-m-d", strtotime($dc->date));
        //}
    //}

}

dcとは?

Dublin Coreの略。
Dublin Coreモジュールとは、標準的なメタデータ記述要素として用いられるDublin CoreのDCMESを利用するモジュールです。

2,170 views

xmlを生成する

// Create XML Object
$xml = new DOMDocument("1.0", "UTF-8");

// Make a Parent Node
$dataset = $xml->createElement("dataset");
$xml->appendChild($dataset);

$data = $xml->createElement("data");
$dataset->appendChild($data);

// make child node
$data->appendChild($xml->createElement("key", $value));

// Show Data
echo $xml->saveXML();
944 views

xml形式の文字列からxmlオブジェクトを生成する

$xmlstr = file_get_contents("[XMLファイル名]");
if(!$dom = domxml_open_mem($xmlstr))
{
    echo "Error while parsing the document\n";
    exit;
}
$root = $dom->document_element();
$childnodes = $root->child_nodes();
foreach ($childnodes as $value)
{
    $nodeArray[$value->tagname] = $value->get_content();
    $chnode = $value->child_nodes();

    foreach($chnode as $v)
    {
        echo $v->tagname . " : " . $v->get_content() . "<br />";
    }

    echo "<br />";
}
1,151 views

PHPでXMLパーサ

例)

$dom = new domDocument;
$dom->load('books.xml'); // XML文書読み込み

$root = $dom->documentElement;
$books = $root->childNodes;

foreach($books as $book)
{
    if(@$book->nodeName == 'book')
    {
        $content = $book->childNodes;
        foreach ($content as $elem)
        {
            if(@$elem->nodeName == 'name')
            {
                $author = $elem->nodeValue;
            }
            if(@$elem->nodeName == 'publish')
            {
                $title = $elem->nodeValue;
            }
        }
    }
}
1,037 views