RE: [PEAR-DEV] Re: [PEPr] +1 for Web Services::Mailman
| From: | Васил Рангелов | Date: | Wed, 18 Jan 2012 15:10:01 +0000 |
| Subject: | RE: [PEAR-DEV] Re: [PEPr] +1 for Web Services::Mailman | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-54724@lists.php.net to get a copy of this message | ||
What part(s?) are you finding horrible? Whether you use SimpleXML or DOM,
you can use XPath once you load the document, and thus accurately and
unambiguously target the elements you need, which is in fact why this is a
preferred approach.
With DOM, the code would look something like the following (in this case for
the unsubscribe functionality) :
<?php
$dom = new DOMDocument;
$dom->loadHTMLFile($url);
$xpath = new DOMXPath($dom);
if ($xpath->query('//h5[.="Successfully Unsubscribed:"]')->length === 1) {
//Successfully unsubscribed!
}
?>
And if your problem is that these are too many statements, you can squeeze
them, like:
<?php
$dom = new DOMDocument;
$xpath = new DOMXPath($dom->loadHTMLFile($url));
if ($xpath->query('//h5[.="Successfully Unsubscribed:"]')->length === 1) {
//Successfully unsubscribed!
}
?>
Or if you sacrifice compatibility with versions prior to PHP 5.4:
<?php
if ((new DOMXPath((new
DOMDocument)->loadHTMLFile($url))->query('//h5[.="Successfully
Unsubscribed:"]')->length === 1) {
//Successfully unsubscribed!
}
?>
The only potential problems, which I'm assuming are the cause for you to
think DOM is horrible, is if you try to load an XHTML page instead of an
HTML one - when you do that, you need to use the XML functions, not the HTML
ones, plus, you must explicitly set the namespace for the XPath to work
properly, i.e.
<?php
$dom = new DOMDocument;
$dom->load($url);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('h', 'http://www.w3.org/1999/xhtml');
if ($xpath->query('//h:h5[.="Successfully Unsubscribed:"]')->length === 1)
{
//Successfully unsubscribed!
}
?>
-----Original Message-----
From: HM 2K [mailto:nfhm2k@gmail.com]
Sent: Wednesday, January 18, 2012 3:37 PM
To: Christian Weiske
Cc: PEAR developer mailinglist
Subject: [PEAR-DEV] Re: [PEPr] +1 for Web Services::Mailman
Hi Christian,
Thanks for the feedback.
You can see here that I've addressed your concerns:
https://github.com/hm2k/Services_Mailman/commits/master
However, I am still considering using simplexml+xpath over regex. I've tried
working with domdocument before to parse html and it's horrible. If it's
easy enough to work with and maintainable I will switch to using
simplexml+xpath instead.
On Sat, Jan 14, 2012 at 12:35 PM, Christian Weiske <cweiske@php.net> wrote:
> Christian Weiske (http://pear.php.net/user/cweiske) has voted +1 on
> the proposal for Web Services::Mailman.
>
> Proposal information:
> http://pear.php.net/pepr/pepr-proposal-show.php?id=672
> Vote information:
>
> http://pear.php.net/pepr/pepr-vote-show.php?id=672&handle=cweiske
>
> This vote is conditional. The condition is:
>
> - Please run phpcs on your code
> - I'd prefer if you used simplexml+xpath instead of regex (and
> dom::loadhtml if necessary)
> - why don't you throw exceptions? currently I have to check the return
> value of every single method call manually instead of putting a
> try-catch around all of it. __construct also does swallow errors that
> occur in the called methods since it cannot return them.
> - I'd remove the default values of your class variables. Put them in
> the docblock
> ("'http://www.example.co.uk/mailman/admin';") or
> in your
> validation ("passwords-cannot-have-spaces").
>