将选定的HTML表格转换为JSON [英] Convert a selected HTML Table to JSON
问题描述
我有这张表:
$ b是否可以将多个表格的HTML选项转换为JSON? $ b
< div class =mon_title> 2.11.2015 Montag< / div>
< table class =info>
< tr class =info>< th class =infoalign =centercolspan =2> Nachrichten zum Tag< / th>< / tr>
< tr class ='info'>< td class ='info'colspan =2>< b>< u>< / u> < / B个
...
< / table>
< p>
< table class =mon_list>
...
< / table>
这个PHP代码将它转换为JSON:
函数save_table_to_json($ in_file,$ out_file){
$ html = file_get_contents($ in_file);
file_put_contents($ out_file,convert_table_to_json($ html));
函数convert_table_to_json($ html){
$ document = new DOMDocument();
$ document-> loadHTML($ html);
$ obj = [];
$ jsonObj = [];
$ th = $ document-> getElementsByTagName('th');
$ td = $ document-> getElementsByTagName('td');
$ thNum = $ th->长度;
$ arrLength = $ td->长度;
$ rowIx = 0;
$ b $ 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 = [];
}
}
save_table_to_json('heute_S.htm','heute_S.json');
它的作用是将 class = info
和表 class = mon_list
并将其转换为json。
有没有什么办法可以把表 class = mon_list
?
值得一提的是,您可以使用 foreach
在$ 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->长度;
$ arrLength = $ td->长度;
$ rowIx = 0;
$ b $ 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 = [];
}
}
Is it possible to convert just a selection of a HTML with multiple tables to JSON ?
I have this Table:
<div class="mon_title">2.11.2015 Montag</div>
<table class="info" >
<tr class="info"><th class="info" align="center" colspan="2">Nachrichten zum Tag</th></tr>
<tr class='info'><td class='info' colspan="2"><b><u></u> </b>
...
</table>
<p>
<table class="mon_list" >
...
</table>
And this PHP code to covert it into JSON:
function save_table_to_json ( $in_file, $out_file ) {
$html = file_get_contents( $in_file );
file_put_contents( $out_file, convert_table_to_json( $html ) );
}
function convert_table_to_json ( $html ) {
$document = new DOMDocument();
$document->loadHTML( $html );
$obj = [];
$jsonObj = [];
$th = $document->getElementsByTagName('th');
$td = $document->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 = [];
}
}
save_table_to_json( 'heute_S.htm', 'heute_S.json' );
What it does is takes the table class=info
and the table class=mon_list
and converts it to json.
Is there any way that it can just take the table class=mon_list
?
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 = [];
}
}
这篇关于将选定的HTML表格转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!