A
A
Anton Kuznetsov2011-12-04 22:08:49
PHP
Anton Kuznetsov, 2011-12-04 22:08:49

PHP: Working with XML

Hello to all habravchanam. The question arose: what is the best way to work with XML in PHP? Interested in both simple exchange of requests (sent an XML request, received a response, processed) and processing large XML files (~ 800mb). It is desirable that the script does not depend heavily on the php settings and installed libraries.
I would be very grateful for code examples.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
M
Melkij, 2011-12-04
@melkij

SimpleXML.
In the kernel by default.

M
Melz, 2011-12-05
@melz

For large files, only pull parsers. SimpleXML, as far as I remember, loads the entire structure into memory, but not very efficiently. Cons: You have to follow things yourself.
So either SAX Parser (http://se.php.net/xml) or XMLReader (http://se.php.net/manual/en/book.xmlreader.php)
By the way, the combined approach works well: looking for a place XMLReader and then load the result into SimpleXML and work with it like normal people.
Do a test, even at 10MB the speed will be noticeable. Try to load even such a file into different parsers and go nuts from the difference in work time and memory usage :)

G
Grigory Peretyaka, 2011-12-05
@Peretyaka

I would like a more detailed task: what is the structure of the document, how often will it be executed, what server, what resources are you ready to use?
As SimpleXML has already said, DOM or SAX indexes everything into memory and 800 MB is not a little. Although on a server with several gigabytes of RAM and with an infrequent request for this functionality, I would do this and not suffer, in any case, processing 800 MB is not an easy operation.
There are a lot of parsers that do not index anything, but go through the file with regular expressions, I tried to use XMLReader, but it slows me down a lot, even after optimizing and loading pieces in SimpleXML, this option did not go into production.
As a result, I wrote my own parser, which read the files sequentially, character by character, creating blocks of “items” directly, which I already loaded into SimpleXML and then processed. I consider this option the only one that works for large files, and tools like XMLReader for very narrow tasks, although I do not rule out that I "just don't know how to cook them."
This is my experience, I absolutely do not pretend to be complete knowledge, but this is a really working option.

V
Vitali Borovik, 2011-12-05
@WAYS

Convention SimpleXML. Here is a simple example.
<?php
$xml = simplexml_load_file("file.xml");
foreach ($xml->items as $item) {
echo $item->name;
echo $item->price;
}
?>

D
Dmitry Pyatkov, 2012-01-16
@dkrnl

I wrote a similar class: dpyatkov.ru/2012/01/12/simplexmlreader/ - a symbiosis of simplexml and xmlreader, it may come in handy.

V
Vitaly Peretyatko, 2011-12-04
@viperet

I think there are no problems with generation, but with parsing - for small XML (request / response) I use Simplexml (simplexml_load_string() ), the resulting object can then be conveniently converted into an associative array. For large XML - xml_parse().

K
knekrasov, 2011-12-05
@knekrasov

Look at SimpleXML

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question