示例#1
0
 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
0
文件: HTML.php 项目: difra-org/difra
 /**
  * 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
0
 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
0
 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
0
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
0
 /**
  * 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
0
<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;
//}