Harry Slaughter
Harry Slaughter

Reputation: 459

Good, solid documentation of PHP DOM

I've been trying to do some simple DOM parsing of HTML documents and am really shocked at how difficult it is to do.

I've looked into some of the many alternatives to PHP's DOM classes (like simple xml parser and simple HTML DOM). I found a very effective dom2array function too, which is useful for extremely basic parsing where you just want raw values of elements.

None of these alternatives is really compelling though.

PHP documentation of the DOM is typically lacking in detail and largely useless. A lot of the comments are actually really helpful though.

The tutorials I've found online typically cover only the very very basics like writing a 20 line XML document or parsing all the p tags in a document. Meh.

Are there any sites (or books) that go into detail specifically on working with the DOM using PHP's DOM libraries?

Upvotes: 1

Views: 145

Answers (3)

Harry Slaughter
Harry Slaughter

Reputation: 459

I should have titled my post, "Easiest way to parse HTML DOM in PHP". 'Easiest' is not a very good word, I know. It's all relative to what you're trying to do. What I'm doing is pretty straight-forward. I want to parse standalone HTML documents and present the content in a different context.

These are the things I wanted to do:

  • Parse basic properties like title and body
  • Alter all file references (images, links, css, js) to point to a valid location
  • Add/remove attributes from tags (dealing with 1995 HTML here)
  • Strip inline styles

I ended up going with Simple HTML DOM Parser

It has a very small learning curve and gives easy read/write access to the DOM. End of story. It does seem to choke on nested elements sometimes though.

Upvotes: 0

phihag
phihag

Reputation: 287885

The DOM is a language-independent interface and documented in detail by the W3C.

That being said, if your aim is extremely simple parsing of (typically) structured information, XML may not be the correct format in the first place; XML includes a variety of advanced features (namespaces, DTDs, XSLT, distinction between attributes and text, markup instead of structured information). If that's the case, consider JSON, which is extremely easy to parse and generate.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798746

Anything that says "DOM" in the name or claims to support it should support the DOM API as defined by the W3C, and you should consider their documentation normative for everything but the language-specific parts.

Upvotes: 0

Related Questions