使用 dom php 创建的不需要的 div
unwanted divs created with dom php
可以创建主要 div 而不显示 xml 标签并且没有任何问题
$myfile = fopen("../userfolders/$email/$ongrassdb/$pagenameselected.php", "a+") or die("Unable to open file!");
$dom = new DOMDocument();
$ele = $dom->createElement('div', $textcon);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-col; width :100%;');
$dom->appendChild($ele);
$html = $dom->saveHTML();
fwrite($myfile,$html);
fclose($myfile);
正在尝试创建子 div,但下面的代码会创建父 div 和子 div 的副本,并在每个 div 之后添加 XML 标签
$myfile = fopen("../userfolders/$email/$ongrassdb/$pagenameselected.php", "a+") or die("Unable to open file!");
$file = "../userfolders/$email/$ongrassdb/$pagenameselected.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$ele = $doc->createElement('div', $textcon);
$element = $doc->getElementsByTagName('div')->item(0);
$element->appendChild($ele);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-cell;');
$doc->appendChild($ele);
$html = $doc->saveHTML();
fwrite($myfile,$html);
将a+改为w+,同时将saveXML改为saveHTML
它将解决问题
谢谢
这里有一个模板,可以随意创建父节点和追加子节点。
<?php
$oDom = new DOMDocument( '1.0', 'UTF-8' );
$oDom->preserveWhiteSpace = false;
$oDom->formatOutput = true;
$iErrorFlag = true;
libxml_use_internal_errors( true );
$oLog = $oDom->createElement( 'log' );
$oDom->appendChild( $oLog );
$oNde = $oDom->createElement( 'lognode' );
$oLog->appendChild( $oNde );
$oNde->appendChild( $oDom->createElement( 'message', 'My message!' ) );
$oNde->appendChild( $oDom->createElement( 'user' , 'My user!' ) );
$oNde->appendChild( $oDom->createElement( 'ip' , 'My ip!' ) );
$bSuccess = file_put_contents( 'logfile.xml', $oDom->saveXML() );
?>
更新
这个怎么样
您将拥有三个文件,$outputfile 将是一个用于保存 html 的新文件。 $parentfile 和 $childfile 是您要从 . $pId是parent的IDdiv,$cId是child的IDdiv(如果没记错应该是在创建文件的时候设置的)
$outputfile = '/path/to/new/output/file/';
$parentfile = '/path/to/file/with/parent/div/';
$childfile = '/path/to/file/with/child/div/';
$pId = 'myParentDivId';
$cId = 'myChildDivId';
$file = $parentfile;
$p = new DOMDocument();
$p->loadHTMLFile($file);
$pEle = $p->getElementById($pId);
$file = $childfile;
$c = new DOMDocument();
$c->loadHTMLFile($file);
$cEle = $c->getElementById($cId);
$pEle->appendChild($cEle);
$m = new DOMDocument();
$m->appendChild($pEle);
$myfile = fopen($outputfile, "a+") or die('bye');
$html = $m->saveHTML();
fwrite($myfile,$html);
fclose($myfile);
可以创建主要 div 而不显示 xml 标签并且没有任何问题
$myfile = fopen("../userfolders/$email/$ongrassdb/$pagenameselected.php", "a+") or die("Unable to open file!");
$dom = new DOMDocument();
$ele = $dom->createElement('div', $textcon);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-col; width :100%;');
$dom->appendChild($ele);
$html = $dom->saveHTML();
fwrite($myfile,$html);
fclose($myfile);
正在尝试创建子 div,但下面的代码会创建父 div 和子 div 的副本,并在每个 div 之后添加 XML 标签
$myfile = fopen("../userfolders/$email/$ongrassdb/$pagenameselected.php", "a+") or die("Unable to open file!");
$file = "../userfolders/$email/$ongrassdb/$pagenameselected.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$ele = $doc->createElement('div', $textcon);
$element = $doc->getElementsByTagName('div')->item(0);
$element->appendChild($ele);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-cell;');
$doc->appendChild($ele);
$html = $doc->saveHTML();
fwrite($myfile,$html);
将a+改为w+,同时将saveXML改为saveHTML
它将解决问题
谢谢
这里有一个模板,可以随意创建父节点和追加子节点。
<?php
$oDom = new DOMDocument( '1.0', 'UTF-8' );
$oDom->preserveWhiteSpace = false;
$oDom->formatOutput = true;
$iErrorFlag = true;
libxml_use_internal_errors( true );
$oLog = $oDom->createElement( 'log' );
$oDom->appendChild( $oLog );
$oNde = $oDom->createElement( 'lognode' );
$oLog->appendChild( $oNde );
$oNde->appendChild( $oDom->createElement( 'message', 'My message!' ) );
$oNde->appendChild( $oDom->createElement( 'user' , 'My user!' ) );
$oNde->appendChild( $oDom->createElement( 'ip' , 'My ip!' ) );
$bSuccess = file_put_contents( 'logfile.xml', $oDom->saveXML() );
?>
更新
这个怎么样
您将拥有三个文件,$outputfile 将是一个用于保存 html 的新文件。 $parentfile 和 $childfile 是您要从 . $pId是parent的IDdiv,$cId是child的IDdiv(如果没记错应该是在创建文件的时候设置的)
$outputfile = '/path/to/new/output/file/';
$parentfile = '/path/to/file/with/parent/div/';
$childfile = '/path/to/file/with/child/div/';
$pId = 'myParentDivId';
$cId = 'myChildDivId';
$file = $parentfile;
$p = new DOMDocument();
$p->loadHTMLFile($file);
$pEle = $p->getElementById($pId);
$file = $childfile;
$c = new DOMDocument();
$c->loadHTMLFile($file);
$cEle = $c->getElementById($cId);
$pEle->appendChild($cEle);
$m = new DOMDocument();
$m->appendChild($pEle);
$myfile = fopen($outputfile, "a+") or die('bye');
$html = $m->saveHTML();
fwrite($myfile,$html);
fclose($myfile);