开发者

What is the use of PHP DOM Manipulation?

Sorry if the question is too general, but I wonder what is the use of a PHP DOM Manipulation compared to javascript DOM Manipulation?

I understood that it was mainly used to parse some html, or xml, but are there other applications where it is good to use?开发者_JS百科


Generally speaking, Javascript is executed on the client-side, in the web-browser.
On the other hand, PHP is executed on the server-side.

Quite often, you'll have to manipulate XML Documents on the server ; in these situations, you'll only have PHP, and will not be able to depend on a browser / on Javascript.


Except from where it's runing, There is not much difference between PHP and Javascript, when it comes to DOM manipulations -- which is a nice thing about DOM : it's quite standardized.


For example, you might want to generate an ATOM or RSS feed (which are XML) for the recent articles on a website ; this will be done on the server-side.

Another example would be a batch script that would process XML-files sent by a bank, every night, containing all payments for the last day ; for such a batch-process, there will be absolutly no browser.


Theres a few you can go for:

The best / easiest is PHP Simple Dom Available at http://simplehtmldom.sourceforge.net/

  • A HTML DOM parser written in PHP5+
  • let you manipulate HTML in a very
  • easy way! Require PHP 5+. Supports
  • invalid HTML. Find tags on an HTML
  • page with selectors just like jQuery.
  • Extract contents from HTML in a
  • single line.

Or alternatively you could use PHPDom, More advanced and more code but more precise coding. Available at: http://php.net/manual/en/book.dom.php

Example:

<?php

$html = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  </head>
  <body>
    <p>&nbsp;</p>
  </body>
</html>
EOF;

// This one works perfectly.
$dom = new DOMDocument();
$dom->loadXML($html, LIBXML_DTDLOAD);

//Do here what you will.

print $dom->saveXML();

?>


The difference is the environment in which you use them: in JavaScript, the DOM you are manipulating is on the client-side - the browser that has loaded the HTML page you are looking at. If you are manipulating a document using the DOM API in PHP, you are doing that that to a document on the server-side.

It seems confusing because the DOM is used in both. This is done just for convenience: PHP implements the DOM commands simply so that one can easily write the same kind of code in PHP as in JavaScript. You cannot use the DOM API support in PHP to manipulate the browser DOM.

(There's an analogy with the local storage APIs here: now that the browsers have local storage APIs, you can use SQL in the browser to store things in the local database - but just because you are writing SQL in the client-side code and in the server-side code doesn't mean they are operating on the same data.)


DOM is a language agnostic interface defined by the W3C.:

The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page.

The purpose of DOM is the same in PHP than it is in JavaScript. But since using DOM in JavaScript usually happens in the browser and has an immediate visible effect on the DOM, it may seem different to you. Of course, it makes no sense to implement an animation with PHP DOM because of that. But keep in mind that an animation is still nothing else but dynamic access and update of the content.

See the link for further information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜