0

XML解析中にいくつかの相互に関連する関数から変数を取得し、それらを配列に入れようとしています。コードは次のとおりです。

function readChapters($reader) {
    while($reader->read()) {
        if( /* condition here */ ) {
            $chapter = readValue($reader);
        }
        if( /* condition here */ ) {
            readModules($reader);
        }
        if( /* condition here */ ) {
            return;
        }
    }
}

function readModules($reader) {
    while($reader->read()) {
        if( /* condition here */ ) {
            readModule($reader);
        }
        if( /* condition here */ ) {
            return($reader);
        }
    }
}

function readModule($reader) {
    while($reader->read()) {
        if( /* condition here */ ) {
            $topic = readValue($reader);
        }
        if( /* condition here */ ) {
            $description = readValue($reader);
        }
    }
}

function readValue($reader) {
    while($reader->read()) {
        if( /* condition here */ ) {
            return $reader->readInnerXML();
        }
    }
}

$reader = new XMLReader();
$reader->open('example.xml');

$current = 0;
$topics_list = array();

$chapterName = "";          // want to add $chapter
$topicName = "";            // want to add $topic
$descriptionText = "";      // want to add $description

while($reader->read()) {
    if(// condition here) {
        readChapters($reader);
    }
    $topics_list[$current] = array();
    $topics_list[$current]['chapter'] = $chapterName;
    $topics_list[$current]['topic'] = $topicName;
    $topics_list[$current]['description'] = $descriptionText;
}
$reader->close();
print_r($topics_list);

問題:これらの関数の外部から$ chapter$ topic$ description変数を取得して、それらを配列に配置するにはどうすればよいですか?前もって感謝します。

更新: XMLドキュメント構造はここにあり、Array()の予想される構造は次のとおりです。

Array (
            [0] => Array (
                    [chapter] => Chapter_name1
                    [topic] => Topic_name1
                    [description] => Content_of_the_topic1
                )
            [1] => Array (
                    [chapter] => Chapter_name1
                    [topic] => Topic_name2
                    [description] => Content_of_the_topic2
                )
            [2] => Array (
                    [chapter] => Chapter_name2
                    [topic] => Topic_name2
                    [description] => Content_of_the_topic2
            )
            .....
        )
4

1 に答える 1

0

基本的に、一連の関数を使用して、XMLオブジェクトのデータを使用してデータ構造を構築しています。つまり、各関数は名前が付けられたデータ構造を返す必要があります。readChapters()はチャプター構造を返す必要があります(1つのチャプターしか読み取っていないと思うので、おそらくreadChapter()という名前にする必要があります)。XMLがどのように見えるか、または目的のデータ構造がどのように見えるかはわかりませんが、次のようなものが必要になります。

function readChapter($reader) {
    $chapter = array();
    while (// condition) {
         if (// something)
              $chapter['chapter'] = readValue($reader);
         elseif (// something else)
              $chapter['topic'] = readValue($reader);
         // etc
    }

    return $chapter;
}

次に、以下のメインループで、次のことができます。

while ($reader->read()) {
    if (// condition here) {
        $topics_list[] = readChapter($reader);
    }
}

それがあなたがあなたが構築できる何かにあなたを近づけることを願っています!

于 2013-01-26T17:56:04.243 に答える