简体   繁体   English

如何使用php DomXpath解析html,修改并保存

[英]How to parse html with php DomXpath, modify and save

After googling could not found anything related to my issue. 谷歌搜索后找不到与我的问题有关的任何内容。 Problem is: I parse page, find one table [there is four tables]. 问题是:我解析页面,找到一个表[有四个表]。

And when I found, I want to add one/some row/rows to table. 当我找到时,我想向表中添加一个/一些行/行。 But I don`t know how to do it. 但是我不知道该怎么做。 Some similar issues are about parsing xml and viewing content. 一些类似的问题与解析xml和查看内容有关。

In code I have something like this: 在代码中,我有这样的东西:

$dom = new DOMDocument();
$dom->loadHTML($output->getHTML());
$xpath = new DOMXPath($dom);
$tableProp = $xpath->query('//*[@class="smwb-factbox"][2]');
....
$dom->asHTML();

Solution is simple: With set of methods such as createElement, setAttribute and appendChild I solved my problem, example as follows: 解决方案很简单:使用诸如createElement,setAttribute和appendChild之类的方法集可以解决我的问题,示例如下:

$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($output->getHTML(), 'HTML-ENTITIES', 'utf-8'));
$xpath = new DOMXPath($dom);
$tableProp = $xpath->query('//*[@class="smwb-factbox"][2]');
...
$th_el = $dom->createElement('th', $th_outer_inner_span_a_el);
...
$td_el = $dom->createElement('td', '');
$td_el->appendChild($td_el_outer_span);
$tr_el = $dom->createElement('tr', '');
$tr_el->setAttribute('class', 'smwb-propvalue');
$tr_el->appendChild($th_el);
$tr_el->appendChild($td_el);
$tableProp->item(0)->appendChild($tr_el);
$dom->saveHTML();
...

The idea is pretty simple. 这个想法很简单。 I have table in mediawiki, find it, create new row and insert it, after save it. 我在mediawiki中有表格,找到它,保存后创建新行并插入它。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM