Convert A Selected Html Table To Json
Solution 1:
You can use XPath to search for the class, and then create a new DOM document that only contains the results of the XPath query. This is untested, but should get you on the right track.
It's also worth mentioning that you can use foreach
to iterate over the node list.
$document = new DOMDocument();
$document->loadHTML( $html );
$xpath = new DomXPath($document);
$tables = $xpath->query("//*[contains(@class, 'mon_list')]");
$tableDom = new DomDocument();
$tableDom->appendChild($tableDom->importNode($tables->item(0), true));
$obj = [];
$jsonObj = [];
$th = $tableDom->getElementsByTagName('th');
$td = $tableDom->getElementsByTagName('td');
$thNum = $th->length;
$arrLength = $td->length;
$rowIx = 0;
for ( $i = 0 ; $i < $arrLength ; $i++){
$head = $th->item( $i%$thNum )->textContent;
$content = $td->item( $i )->textContent;
$obj[ $head ] = $content;
if( ($i+1) % $thNum === 0){
$jsonObj[++$rowIx] = $obj;
$obj = [];
}
}
Solution 2:
Another unrelated answer is to use getAttribute()
to check the class name. Someone on a different answer has written a function for doing this:
functiongetElementsByClass(&$parentNode, $tagName, $className) {
$nodes=array();
$childNodeList = $parentNode->getElementsByTagName($tagName);
for ($i = 0; $i < $childNodeList->length; $i++) {
$temp = $childNodeList->item($i);
if (stripos($temp->getAttribute('class'), $className) !== false) {
$nodes[]=$temp;
}
}
return$nodes;
}
Element?
I have a table and I need the cells to have an element posi…
Fixed Header Div With Scrollable Div Below
I'm trying to place two divs one above the other. The t…
If Div Contains Specific Word Add A New Class In Parent Div Using Jquery
This is the HTML code: Offer And here's the jquery…
How To Make A Tumblr Theme (specifically Corner Images) Adjust To Different Screen Resolutions?
I have a pleasant theme on tumblr ( mostlylagomorph.tumblr.…
Show All "title" Tooltips At Once On One Page
Thank you for the answers. I edited my question though: I&#…
How To Make A Transition Effect Up The Input On Change
I need to place this particular effect on a dropdown I need…
How To Limit The Html Table Records In Each Page
I am populating a table in my jsp which has more than 20 re…
Displaying Validation Error Instead Of Default Text Errors In Django Usercreationform
I have a very simple registration form on my website. My fo…
|
Post a Comment for "Convert A Selected Html Table To Json"