本文整理汇总了PHP中DOMDocument::normalize方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::normalize方法的具体用法?PHP DOMDocument::normalize怎么用?PHP DOMDocument::normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::normalize方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: assertXMLEquals
protected function assertXMLEquals($expect, $test)
{
$doc = new DOMDocument();
$this->assertTrue($doc->loadXML($expect), "Can load {$expect}");
$doc->normalize();
$expect = $doc->saveXML();
$doc = new DOMDocument();
$this->assertTrue($doc->loadXML($test), "Can load {$test}");
$doc->normalize();
$test = $doc->saveXML();
$this->assertEquals($expect, $test);
}
示例2: process
/**
* HTML processor
* @param string $source Source HTML
* @param bool $clean Perform cleaning
* @param bool $withHeaders Return full HTML page (true) or contents only (false)
* @return string
*/
public function process($source, $clean = true, $withHeaders = false)
{
if (!trim($source)) {
return '';
}
/*try {
$source = \Difra\Libs\ESAPI::encoder()->canonicalize( $source );
} catch( \Exception $ex ) {
return false;
}*/
// html to dom conversion
$html = new \DOMDocument('1.0');
libxml_use_internal_errors(true);
$html->loadHTML('<?xml version = "1.0" encoding = "utf-8"?>' . $source);
libxml_use_internal_errors(false);
$html->normalize();
// clean dom
if ($clean) {
$bodyList = $html->documentElement->getElementsByTagName('body');
if ($bodyList->length and $bodyList->item(0)->childNodes->length) {
$body = $bodyList->item(0);
$replaceNodes = [];
foreach ($body->childNodes as $node) {
$newReplaceNodes = $this->clean($node);
$replaceNodes = array_merge($replaceNodes, $newReplaceNodes);
}
if (!empty($replaceNodes)) {
foreach ($replaceNodes as $replaceNode) {
$this->replace($replaceNode);
}
}
} else {
return false;
}
}
// dom to html conversion
if ($withHeaders) {
$output = $html->saveHTML();
} else {
$newDom = new \DOMDocument();
foreach ($html->documentElement->childNodes as $node) {
if ($node->nodeName == 'body') {
foreach ($node->childNodes as $subNode) {
$newDom->appendChild($newDom->importNode($subNode, true));
}
}
}
$output = $newDom->saveHTML();
// TODO: convert xhtml to html5
}
return mb_convert_encoding($output, 'UTF-8', 'HTML-ENTITIES');
}
示例3: xmlDump
public static function xmlDump($var)
{
$oDM = new DOMDocument();
$oDM->loadXML($var);
if ($oDM) {
$oDM->formatOutput = true;
$oDM->normalize();
$var = $oDM->saveXML();
$widget = new CTextHighlighter();
$widget->language = 'xml';
echo $widget->highlight(${$var});
}
}
示例4: formatXML
public static function formatXML($xml)
{
libxml_use_internal_errors(true);
$oDM = new DOMDocument();
try {
$load = @$oDM->loadXML($xml);
if ($load) {
$oDM->formatOutput = true;
$oDM->normalize();
$xml = $oDM->saveXML();
}
} catch (Exeption $e) {
}
return $xml;
}
示例5: toXML
function toXML()
{
global $sheet, $file;
if ($sheet === null) {
return;
}
$doc = new DOMDocument('1.0', 'utf-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$root = $doc->createElement("character");
$doc->appendChild($root);
$gear = $doc->createElement("gear");
$root->appendChild($gear);
$fileName = $file;
foreach ($sheet as $key => $value) {
switch ($key) {
case "monk":
case "priest":
case "shaman":
case "druid":
case "warlock":
case "warrior":
case "hunter":
case "deathknight":
case "rogue":
case "mage":
case "paladin":
$fileName = $value["value"];
$root->setAttribute("name", $value["value"]);
$root->setAttribute("class", $key);
break;
case "level":
$root->setAttribute("level", $value["value"]);
break;
case "race":
$root->setAttribute("race", $value["value"]);
break;
case "role":
$root->setAttribute("role", $value["value"]);
break;
case "head":
case "neck":
case "shoulder":
case "chest":
case "waist":
case "legs":
case "feet":
case "wrist":
case "finger1":
case "finger2":
case "trinket1":
case "trinket2":
case "back":
case "main_hand":
case "off_hand":
$e = $doc->createElement("item");
$e->setAttribute("slot", $key);
$vars = $value["vars"];
foreach ($vars as $vkey => $vvalue) {
$e->setAttribute($vkey, $vvalue);
}
$e->appendChild($doc->createTextNode($value["value"]));
$gear->appendChild($e);
break;
default:
}
}
$doc->normalize();
$length = strrpos($fileName, ".");
if (!$length) {
$length = strlen($fileName);
}
$f = substr($fileName, 0, $length) . ".xml";
$doc->save($f);
return $doc;
}
示例6: prettyprint
/**
* prettyprint -- get rid of superfluous namespace declarations
* @param DOMDocument $dom
* @return DOMDocument return cleaned DOM
*/
private function prettyprint($dom)
{
$dom->normalize();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$outXML = $dom->saveXML();
$dom->loadXML($outXML, LIBXML_NSCLEAN);
return $dom;
}
示例7:
<txt>
\t<txt lines="1">
\t\t<txt lines="nope"></txt>
\t</txt>
\t<txt lines="0">
\t\t<txt lines="12"></txt>
\t <txt lines="3">
\t <txt lines-after="2"></txt>
\t <txt></txt>
\t </txt>
\t\t<txt lines-before="2"></txt>
\t</txt>
</txt>
EOS
);
$dom->normalize();
$xpath = new \DOMXPath($dom);
$root = $xpath->query('/txt')->item(0);
//$r = $xpath->evaluate(
//'.//txt[last() and ancestor::txt[last()]]',
//$root
//);
//$it = $root;
//$lines = 0;
//while(($it = $it->lastChild )&& $it instanceof DOMElement) {
//var_dump($it->getNodePath());
//if($it->hasAttribute('lines')) {
//$line = (int) $it->getAttribute('lines');
//var_dump($line);
//if($line > $lines) $lines = $line;
//}