1

PHP curlを使用してリモートURLからxmlファイルを取得し、サーバー上のローカルファイルに保存しています。構造は次のとおりです。

<Store Country="Ireland">
<EventsPoints>
    <Event ID="1800" >
        <ArtistIDs>
            <ArtistID ID="109" Type="Primary" />
        </ArtistIDs>
        <CategoryID>1</CategoryID>
        <Country>IRL</Country>
        <PerformanceName>Music and Arts</PerformanceName>
        <VenueID ID="197" />
    </Event>
<Venues>
    <Venue ID="197">
        <City>Dublin</City>
        <Country>IRL</Country>
        <VenueName>ABC</VenueName>
        <VenueNumber>22</VenueNumber>
    </Venue>
</Venues>

上記のxmlブロックは同じXMLファイルに保存されます。いくつかのイベントブロックといくつかの会場ブロックがあります。私が抱えている問題は、PHPを使用してこの大きなXMLファイルにアクセスし、Venueブロックを反復処理して、パラメーターで指定された特定のIDを持つVenueブロックのみを取得することです。次に、イベントブロックを反復処理します。この指定された会場IDに一致するイベントのみを取得します。次に、これをサーバー上のファイルに保存します。会場ごとにやりたいです。

上記を行うにはどうすればよいですか?

編集:

各会場とその会場に関連するイベントについて、私はそれらを文字通り独自のファイルにコピーしたいだけです-基本的に大きなファイルを個々のファイルに分割します

 $docSource = new DOMDocument();
$docSource->loadXML($xml);
$docDest = new DOMDocument();
$docDest->loadXML(file_get_contents('/var/www/html/xml/testfile.xml'));

$xpath = new DOMXPath($docSource);
$id = "197";
$result = $xpath->query('//Event/VenueID[@ID=$id]')->item(0); //Get directly the node you want

$result = $docDest->importNode($result, true); //Copy the node to the other document

$items = $docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result); //Add the copied node to the destination document

echo $docDest->saveXML();
4

1 に答える 1

0

目的の出力形式が表示されていないため、これで目的の出力が生成されると思います。そうでない場合は、目的の出力形式に合うようにコードを自由に変更してください。これと上記のコメントは、これを自分で機能させるために必要なすべてを備えているはずです。

// load Source document
$srcDom = new DOMDocument;
$srcDom->load('/var/www/html/xml/testfile.xml');
$xPath = new DOMXPath($srcDom);
// iterate over all the venues in the source document
foreach ($srcDom->getElementsByTagName('Venue') as $venue) {
    // create a new destination document for the current venue
    $dstDom = new DOMDocument('1.0', 'utf-8');
    // add an EventsPoint element as the root node
    $dstDom->appendChild($dstDom->createElement('EventsPoint'));
    // import the Venue element tree to the new destination document
    $dstDom->documentElement->appendChild($dstDom->importNode($venue, true));
    // fetch all the events for the current venue from the source document
    $allEventsForVenue = $xPath->query(
        sprintf(
            '/Store/EventsPoints/Event[VenueID/@ID=%d]',
            $venue->getAttribute('ID')
        )
    );
    // iterate all the events found in Xpath query
    foreach ($allEventsForVenue as $event) {
        // add event element tree to current destination document
        $dstDom->documentElement->appendChild($dstDom->importNode($event, true));
    }
    // make output prettier
    $dstDom->formatOutput = true;
    // save XML to file named after venue ID
    $dstDom->save(sprintf('/path/to/%d.xml', $venue->getAttribute('ID')));
}

これにより、次のようなXMLファイルが作成されます

<?xml version="1.0" encoding="utf-8"?>
<EventsPoint>
  <Venue ID="197">
                <City>Dublin</City>
                <Country>IRL</Country>
                <VenueName>ABC</VenueName>
                <VenueNumber>22</VenueNumber>
            </Venue>
  <Event ID="1800">
            <ArtistIDs>
                <ArtistID ID="109" Type="Primary"/>
            </ArtistIDs>
            <CategoryID>1</CategoryID>
            <Country>IRL</Country>
            <PerformanceName>Music and Arts</PerformanceName>
            <VenueID ID="197"/>
        </Event>
</EventsPoint>

ファイル197.xml内

于 2012-05-19T11:38:27.563 に答える