27 ms·
If only there was some way to address XML/HTML elements by their location in the DOM tree. The "path", if you will. We could call it... XPath.
by jrockway 16y ago
If only there was some way to address XML/HTML elements by their location in the DOM tree. The "path", if you will. We could call it... XPath.
- revicon 16y agoI'll bet you a nickle that most of the pages you'd use this on are not properly formed XML.
- pjscott 16y agoNot as big a problem as you might think. Just feed them to a parser designed to deal with broken HMTL, and then use XPath. I'm using lxml in Python for this, and it's working very well for me.
- timrobinson 16y agoLikewise on .NET, the HTML Agility Pack: http://htmlagilitypack.codeplex.com/ http://htmlagilitypack.codeplex.com/
- jrockway 16y agoNot a big deal. You can make a DOM from anything. In fact, the invalid case is exactly why you'd use this technique. If you use a bunch of regular expressions, you can probably make something that works on a subset of valid inputs. If you want it to work on everything, then you need to use a parser to generate a DOM, and then interact with the DOM.
- pkulak 16y agoOr even CSS. Nokogiri lets you use both, even on badly formed pages.