Removing inline elements when importing HTML into DOMDocument or SimpleXML?

TunE

New Member
I have an external HTML source that I want to scrape and either transform into a local XML file or add to a MySQL DB.The external source is mostly normalized and (somewhat) semantic, so that all I need to do is use \[code\]XPATH\[/code\] to get all \[code\]td\[/code\] content or all \[code\]li\[/code\] content, etc. The problem is that occasionally these items use \[code\]<strong>\[/code\] or \[code\]<b>\[/code\] or \[code\]<i>\[/code\] tags to style the elements I need.This is technically semantic, since the point is to add emphasis to the specific text, and the developer might want to use CSS that isn't the browser default.The problem is that the actual content I am trying to grab is considered a child of this inline element, so that PHP extensions like \[code\]simplexml\[/code\] or \[code\]DOMDocument\[/code\] and \[code\]DOMNode\[/code\] treat them as such. For example:\[code\]<table><tr><td>Thing 1</td><td>Thing 2</td></tr><tr><td>Thing 3</td><td>Thing 4</td></tr><tr><td><strong>Thing 5</strong></td><td><strong>Thing 6</strong></td></tr></table>\[/code\]Will result in:\[code\]
=> Thing 1 [td] => Thing 2 [tr] => [td] => Thing 3 [td] => Thing 4 [tr] => [td] => [strong] => Thing 5 [td] => [strong] => Thing 6\[/code\]Obviously the above is not quite what simplexml returns, but the above reflects the general problem.So is there a way, using either a parameter already built into DOMDocument or using an extra sophisticated XPath query to get the contents of the \[code\]td\[/code\] element with any children (if there are any) stripped of their descendant status and all content treated as the text of the queried element?Right now, the only solutions I have are to either:a) have a \[code\]foreach\[/code\] loop that checks each result, like:\[code\]$result_text = ($result -> strong) ? $result - strong : $result;\[/code\]b) using regex to strip any \[code\]<strong>\[/code\] tags out of the HTML string before importing it into any pre-built classes like simplexml or DOMDocument.
=> =>
 
Back
Top