如何在 PHP 中使用 XMLReader? [英] How to use XMLReader in PHP?

查看:42
本文介绍了如何在 PHP 中使用 XMLReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 文件,该文件相当大,我无法让 simplexml 打开和读取文件,所以我正在尝试 XMLReader,但在 php 中没有成功

I have the following XML file, the file is rather large and i haven't been able to get simplexml to open and read the file so i'm trying XMLReader with no success in php

<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
    <last_updated>2009-11-30 13:52:40</last_updated>
    <product>
        <element_1>foo</element_1>
        <element_2>foo</element_2>
        <element_3>foo</element_3>
        <element_4>foo</element_4>
    </product>
    <product>
        <element_1>bar</element_1>
        <element_2>bar</element_2>
        <element_3>bar</element_3>
        <element_4>bar</element_4>
    </product>
</products>

很遗憾,我没有找到关于 PHP 的好的教程,我很想知道如何将每个元素的内容存储在数据库中.

I've unfortunately not found a good tutorial on this for PHP and would love to see how I can get each element content to store in a database.

推荐答案

这完全取决于工作单元的大小,但我猜你正在尝试处理每个 <product/> 节点连续.

It all depends on how big the unit of work, but I guess you're trying to treat each <product/> nodes in succession.

为此,最简单的方法是使用 XMLReader 访问每个节点,然后使用 SimpleXML 访问它们.这样,您可以将内存使用量保持在较低水平,因为您一次只处理一个节点,并且您仍然可以利用 SimpleXML 的易用性.例如:

For that, the simplest way would be to use XMLReader to get to each node, then use SimpleXML to access them. This way, you keep the memory usage low because you're treating one node at a time and you still leverage SimpleXML's ease of use. For instance:

$z = new XMLReader;
$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'product')
{
    // either one should work
    //$node = new SimpleXMLElement($z->readOuterXML());
    $node = simplexml_import_dom($doc->importNode($z->expand(), true));

    // now you can use $node without going insane about parsing
    var_dump($node->element_1);

    // go to next <product />
    $z->next('product');
}

快速概览不同方法的优缺点:

Quick overview of pros and cons of different approaches:

仅限 XMLReader

  • 优点:速度快,占用内存少

  • Pros: fast, uses little memory

缺点:编写和调试过于困难,需要大量用户空间代码才能做任何有用的事情.Userland 代码很慢并且容易出错.另外,它为您留下了更多的代码行来维护

Cons: excessively hard to write and debug, requires lots of userland code to do anything useful. Userland code is slow and prone to error. Plus, it leaves you with more lines of code to maintain

XMLReader + SimpleXML

  • 优点:不使用太多内存(仅处理一个节点所需的内存),顾名思义,SimpleXML 非常易于使用.

  • Pros: doesn't use much memory (only the memory needed to process one node) and SimpleXML is, as the name implies, really easy to use.

缺点:为每个节点创建一个 SimpleXMLElement 对象的速度不是很快.您确实必须对其进行基准测试以了解它是否对您来说是个问题.不过,即使是一台普通的机器也能每秒处理一千个节点.

Cons: creating a SimpleXMLElement object for each node is not very fast. You really have to benchmark it to understand whether it's a problem for you. Even a modest machine would be able to process a thousand nodes per second, though.

XMLReader + DOM

  • 优点:使用的内存与 SimpleXML 差不多,XMLReader::expand() 比创建新的 SimpleXMLElement 更快.我希望可以使用 simplexml_import_dom() 但它似乎在这种情况下不起作用

  • Pros: uses about as much memory as SimpleXML, and XMLReader::expand() is faster than creating a new SimpleXMLElement. I wish it was possible to use simplexml_import_dom() but it doesn't seem to work in that case

缺点:DOM 使用起来很烦人.它介于 XMLReader 和 SimpleXML 之间.不像 XMLReader 那样复杂和笨拙,但与 SimpleXML 的工作相去甚远.

Cons: DOM is annoying to work with. It's halfway between XMLReader and SimpleXML. Not as complicated and awkward as XMLReader, but light years away from working with SimpleXML.

我的建议:用 SimpleXML 写一个原型,看看它是否适合你.如果性能是最重要的,请尝试 DOM.尽可能远离 XMLReader.请记住,您编写的代码越多,引入错误或性能下降的可能性就越大.

My advice: write a prototype with SimpleXML, see if it works for you. If performance is paramount, try DOM. Stay as far away from XMLReader as possible. Remember that the more code you write, the higher the possibility of you introducing bugs or introducing performance regressions.

这篇关于如何在 PHP 中使用 XMLReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆