【问题标题】:PHP - Retrieving Only Certain XML Nodes and Saving to a filePHP - 仅检索某些 XML 节点并保存到文件
【发布时间】:2012-05-19 10:41:15
【问题描述】:

我正在使用 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文件中。有几个 Event 块和几个 Venue 块。 我遇到的问题是使用 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();

【问题讨论】:

  • 使用 XPath://Event/VenueID[@ID=$id]
  • 那么我是否将其放入 for each 循环中,然后保存到单独的文件中?
  • 是的。看看这个XPath tutorial
  • 我如何将其保存为 XML 格式?对于与该场地相关的每个场地和活动,我只想将它们复制到自己的文件中 - 基本上将较大的文件拆分为单独的文件
  • 请根据具体要求更新您的问题。也许显示所需的输出文件并解释您面临的问题。或许可以展示你到目前为止所尝试过的内容。

标签: php xml


【解决方案1】:

您没有显示所需的输出格式,所以我假设这会生成您想要的。如果没有,请随意修改代码,使其符合所需的输出格式。这连同上面的 cmets 应该有你自己工作所需的一切。

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

【讨论】:

  • 是的,这就是我正在寻找的输出。我现在收到此错误:XML Parsing Error: junk after document element, Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument,
  • @user1083813 确保您的源 XML 从一开始就有效
  • 如何使用not() xpath 函数并删除所有不需要的?
  • 源 XML 来自我无法编辑的远程服务器,所以有没有办法禁用警告检查?​​
  • @user1083813 您可以禁用解析错误,但这不会使损坏的 XML 有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2019-09-29
相关资源
最近更新 更多