用dom php创建的不需要的div

Can create primary div without xml tags displaying and without any problem

$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);

Trying to create child div but the below code creates duplicates of parent div and child div and adds XML tags after every div

$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);

update

How about this

You will have three files, $outputfile will be a new file to save the html to. the $parentfile and $childfile are the two files you want to pull divs out of . $pId is the ID of the parent div, $cId is the ID of the child div (which should have been set when the files were created if im not mistaken)

$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);

Change a+ to w+, Also change saveXML to saveHTML

It will solve the issues

Thanks

Here's a template to create a parent and append child nodes at will.

<?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() );
?>