【青空文庫】青空文庫のXMLをパースしてみた
自分の読書用に青空文庫に掲載してあるXMLをパースしてみたよ。
青空文庫のXMLは仕様通りにキッチリ書いてあるものと、そうでないものがあり、ちょっと苦労した。
以下のクラスは基本的に「大見出し」と「中見出し」で区切って配列にして返すものだけど、面倒な処理は正規表現やexplodeでばっさり切り分けたりしてます。
---
このクラスを使って小説を転載したものは、以下のサイトで読めます。
 ・
多賀城[たがのき]小説投稿サイト
良かったら見てくださいませ。
---
/*
*   青空文庫XMLパーサ
*     2013/10/01
*/
class Aozora
{
  const         FROM_ENCODING = 'sjis-win';
  const         TO_ENCODING   = 'UTF-8';
  
  public        $filename;
  public        $title;
  public        $author;
  /*
    本文の階層構造
    
    大見出しがある場合
    bodies = array(
      '大見出し_01' => array( '中見出し_01' => '本文', '中見出し_02' => '本文', ... '中見出し_nn' => '本文' ),
      '大見出し_02' => array( '中見出し_01' => '本文', '中見出し_02' => '本文', ... '中見出し_nn' => '本文' ),
      '大見出し_03' => array( '中見出し_01' => '本文', '中見出し_02' => '本文', ... '中見出し_nn' => '本文' ),
              ...
      '大見出し_nn' => array( '中見出し_01' => '本文', '中見出し_02' => '本文', ... '中見出し_nn' => '本文' ) );
      
    大見出しが無い場合
    bodies = array( '中見出し_01' => '本文', '中見出し_02' => '本文', ... '中見出し_nn' => '本文' );
  */
  public        $bodies;
  public        $infomation;          // 小説情報
  public        $notes;               // 注
  public        $is_omidasi;
  public        $is_tyumidasi;
  
  private       $raw_text;
  private       $main_text;
  private       $biblio;
  
  public function __construct( $filename )
  {
    $this->filename = $filename;
    $this->raw_text = file_get_contents( $this->filename );
    $this->raw_text = mb_convert_encoding( $this->raw_text, self::TO_ENCODING, self::FROM_ENCODING );
    $this->bodies = array();
  }
  
  public function get_title()
  {
    return sprintf( '%s (作者:%s)', $this->title, $this->author );
  }
  
  public function get_bodies()
  {
    return $this->bodies;
  }
  
  /**
    XMLパース本体
  */
  public function parse()
  {
    // 改行を全て消す
    $this->raw_text = preg_replace( '#[\n\r]+#i', '', $this->raw_text );
    $this->raw_text = preg_replace( '#[\s]+#i', ' ', $this->raw_text );
    
    // タイトルと著者を取得する
    $ret = preg_match( '#(.*?)
#i', $this->raw_text, $temp_text );
    $this->title = $this->replace_imgtag( $temp_text[ 1 ] );
    $this->title = trim( $this->title );
    $ret = preg_match( '#(.*?)
#i', $this->raw_text, $temp_text );
    $this->author = $this->replace_imgtag( $temp_text[ 1 ] );
    $this->author = trim( $this->author );
    
    // 付加情報を取得する
    $ret = preg_match( '#(.*?)
#i', $this->raw_text, $temp_text );
    $this->infomation = $this->remove_htmltag( $temp_text[ 1 ] );
    $this->infomation = $this->add_return( $this->infomation );
    $this->infomation = trim( $this->infomation );
    
    // 本文を全て取得する
    $ret = preg_match( '#(.*)