PHP - 仅检索某些 XML 节点并保存到文件


PHP - Retrieving Only Certain XML Nodes and Saving to a file

我正在使用 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();

您没有显示所需的输出格式,因此我将假设这会生成您想要的内容。如果没有,请随意修改代码,使其符合所需的输出格式。这与上面的评论一起应该拥有您自己完成这项工作所需的一切。

// 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