本文整理汇总了PHP中DOMDocument::normalizeDocument方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::normalizeDocument方法的具体用法?PHP DOMDocument::normalizeDocument怎么用?PHP DOMDocument::normalizeDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::normalizeDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: threadParse
protected function threadParse($raw)
{
$xml = new SimpleXMLElement($raw);
$json = $xml->json;
$html = $this->ampsfix($xml->html);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom->normalizeDocument();
$xpath = new DOMXPath($dom);
$array = [];
$imgs = $xpath->evaluate('/html/body//div/div/div/table/tr/td/div/table/tr/td/table/tr/td/div/img');
$i = 0;
foreach ($imgs as $img) {
$t = explode('?', $img->getAttribute('src'));
$array[$i]['user']['avatar'] = $t[0];
$i++;
}
$ppl = $xpath->evaluate('/html/body//div/div/div/table/tr/td/div/table/tr/td/table/tr/td/div/span/a');
$i = 0;
foreach ($ppl as $pp) {
$t = $pp->nodeValue;
$array[$i]['user']['name'] = $t;
$i++;
}
return $array;
}
示例2: stringMatches
/**
* {@inheritdoc}
*/
protected function stringMatches($other)
{
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;
if (!@$dom->loadXML($other, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
libxml_disable_entity_loader($disableEntities);
$this->setXMLConstraintErrors();
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
return false;
}
$dom->normalizeDocument();
libxml_disable_entity_loader($disableEntities);
libxml_clear_errors();
if (false === ($result = @$dom->schemaValidateSource($this->XSD))) {
$this->setXMLConstraintErrors();
}
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
return $result;
}
示例3: parse
/**
* Parses the given template content
*
* Calls cleanupParse on failure or success
*
* @param string $contents the source content
*
* @return string the compiled form
*/
protected function parse($contents)
{
if (isset($this->buffer)) {
throw new RuntimeException('PHPSTLCompiler->parse called recursivley');
}
try {
$this->whitespace = self::WHITESPACE_COLLAPSE;
$this->stash = array();
$this->meta = array();
$this->buffer = '';
$this->footerBuffer = '';
$this->handlers = array();
$this->dom = new DOMDocument();
$this->dom->preserveWhiteSpace = true;
$this->meta['uri'] = (string) $this->template;
$this->meta['type'] = 'text/html';
if (!$this->dom->loadXML($contents)) {
die("failed to parse {$this->template}");
}
$this->dom->normalizeDocument();
$this->writeTemplateHeader();
$this->process($this->dom);
$this->writeTemplateFooter();
$meta = $this->meta;
$content = $this->unstashPHP(trim($this->buffer));
$this->cleanupParse();
return array($meta, $content);
} catch (Exception $ex) {
$this->cleanupParse();
throw $ex;
}
}
示例4: __construct
public function __construct($url)
{
if (!preg_match('!^https?://!i', $url)) {
$url = 'http://' . $url;
}
$data = Http::Request($url);
//$enc = mb_detect_encoding($str, "UTF-8,ISO-8859-1,ASCII");
$html = mb_convert_encoding($data, "UTF-8", "UTF-8,ISO-8859-1,ASCII");
//$html = utf8_encode($html);
$r = new Readability($html, $url);
$r->init();
if (!isset($this->metadata["title"])) {
$this->metadata["title"] = CharacterEntities::convert(strip_tags($r->getTitle()->innerHTML));
}
if (!isset($this->metadata["author"])) {
$parts = parse_url($url);
$this->metadata["author"] = $parts["host"];
}
$article = $r->getContent()->innerHTML;
if (substr($article, 0, 5) == "<body") {
$article = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/></head>" . $article . "</html>";
} else {
$article = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/></head><body>" . $article . "</body></html>";
}
$doc = new DOMDocument();
@$doc->loadHTML($article) or die($article);
$doc->normalizeDocument();
$this->images = $this->handleImages($doc, $url);
$this->text = $doc->saveHTML();
}
示例5: parseFile
/**
* Validates and parses the given file into a SimpleXMLElement
*
* @param string $file
* @return SimpleXMLElement
*/
private function parseFile($file)
{
$dom = new \DOMDocument();
$current = libxml_use_internal_errors(true);
if (!@$dom->load($file, LIBXML_COMPACT)) {
throw new \RuntimeException(implode("\n", $this->getXmlErrors()));
}
$location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
$parts = explode('/', $location);
if (preg_match('#^phar://#i', $location)) {
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
if ($tmpfile) {
file_put_contents($tmpfile, file_get_contents($location));
$tmpfiles[] = $tmpfile;
$parts = explode('/', str_replace('\\', '/', $tmpfile));
}
}
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
$location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
$source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
$source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
if (!@$dom->schemaValidateSource($source)) {
throw new \RuntimeException(implode("\n", $this->getXmlErrors()));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
libxml_use_internal_errors($current);
return simplexml_import_dom($dom);
}
示例6: getDocument
private function getDocument()
{
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->load(__DIR__ . '/../resource/base.svg.xml');
$doc->normalizeDocument();
$doc->formatOutput = true;
return $doc;
}
示例7: loadFile
/**
* Loads an XML file.
*
* @param string $file An XML file path
* @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
*
* @return \DOMDocument
*
* @throws \InvalidArgumentException When loading of XML file returns error
*/
public static function loadFile($file, $schemaOrCallable = null)
{
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();
$dom = new \DOMDocument();
$dom->validateOnParse = true;
if (!$dom->loadXML(file_get_contents($file), LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
libxml_disable_entity_loader($disableEntities);
throw new \InvalidArgumentException(implode("\n", static::getXmlErrors($internalErrors)));
}
$dom->normalizeDocument();
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new \InvalidArgumentException('Document types are not allowed.');
}
}
if (null !== $schemaOrCallable) {
$internalErrors = libxml_use_internal_errors(true);
libxml_clear_errors();
$e = null;
if (is_callable($schemaOrCallable)) {
try {
$valid = call_user_func($schemaOrCallable, $dom, $internalErrors);
} catch (\Exception $e) {
$valid = false;
}
} elseif (!is_array($schemaOrCallable) && is_file((string) $schemaOrCallable)) {
$valid = @$dom->schemaValidate($schemaOrCallable);
} else {
libxml_use_internal_errors($internalErrors);
throw new \InvalidArgumentException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
}
if (!$valid) {
$messages = static::getXmlErrors($internalErrors);
if (empty($messages)) {
$messages = array(sprintf('The XML file "%s" is not valid.', $file));
}
throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
}
libxml_use_internal_errors($internalErrors);
}
return $dom;
}
示例8: parse
public function parse($url, $html)
{
$doc = new \DOMDocument();
@$doc->loadHTML($html);
$doc->normalizeDocument();
$main = $doc->getElementById('main');
$classe = array('href' => $url, 'name' => str_replace(' Class', '', $main->getElementsByTagName('h2')->item(0)->nodeValue), 'summary' => $main->getElementsByTagName('p')->item(0)->nodeValue, 'methods' => array());
foreach ($main->getElementsByTagName('article') as $article) {
if ($article->getElementsByTagName('h4')->length) {
$h4 = $article->getElementsByTagName('h4')->item(0);
if (preg_match('/([\\w\\*]+)\\((.*)\\)$/', $h4->nodeValue, $name_args)) {
$args = array();
foreach (explode(',', $name_args[2]) as $arg) {
$arg = str_replace('$', '', trim($arg));
preg_match('/^([^ ]+ )?([^ =]+)( =[^=]*)?$/', $arg, $matches);
if ($matches[3]) {
$args[] = '[' . $matches[2] . ']';
} elseif ($arg) {
$args[] = $matches[2];
}
}
if (count($args)) {
$args = '(' . implode(', ', $args) . ')';
} else {
$args = '()';
}
$access = '';
$return = '';
if ($article->getElementsByTagName('table')->length) {
foreach ($article->getElementsByTagName('table')->item(0)->getElementsByTagName('tbody')->item(0)->getElementsByTagName('tr') as $tr) {
if ($tr->getElementsByTagName('th')->item(0)->nodeValue === 'Static') {
$access = $tr->getElementsByTagName('td')->item(0)->nodeValue === 'Yes' ? '::' : '->';
}
if ($tr->getElementsByTagName('th')->item(0)->nodeValue === 'Returns') {
$return = $tr->getElementsByTagName('td')->item(0)->nodeValue;
break;
}
}
}
$name = $name_args[1];
if (isset($classe['methods'][$name]) && $name === 'pre_save') {
$name = 'pre_update';
}
$classe['methods'][$name] = array('href' => $url . ($access ? '#method_' : '#function_') . $name, 'name' => $name, 'args' => $args, 'access' => $access, 'return' => static::$types[strtolower($return)]);
}
}
}
if (isset($this->classes[$url]['methods'])) {
foreach ($this->classes[$url]['methods'] as $name => $method) {
$classe['methods'][$name] = array_merge($classe['methods'][$name], $this->classes[$url]['methods'][$name]);
}
unset($this->classes[$url]['methods']);
}
$this->classes[$url] = array_merge($classe, $this->classes[$url]);
$this->count[] = $url;
}
示例9: parseFile
/**
* Parses the given file into a SimpleXMLElement
* Does NOT validate the file (much faster)
*
* @param string $file
* @return SimpleXMLElement
*/
protected function parseFile($file)
{
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
if (!$dom->load($file, LIBXML_COMPACT)) {
throw new \Exception(implode("\n", $this->getXmlErrors()));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
libxml_use_internal_errors(false);
return simplexml_import_dom($dom);
}
示例10: cleanup
public function cleanup()
{
$paragraphNodeList = $this->getParagraphNodeList();
/** @var $paragraphNode \DOMNode */
foreach ($paragraphNodeList as $paragraphNode) {
$clonedParagraphNode = $paragraphNode->cloneNode(true);
// fixed missing paragraph props element
$runNodeList = $this->getRunNodeList($clonedParagraphNode);
$runIndex = 0;
$currentRunNode = $runNodeList->item($runIndex);
$runIndex += 1;
$nextRunNode = $runNodeList->item($runIndex);
while ($currentRunNode) {
if ($nextRunNode !== null) {
$isEqual = $this->deepEqual($this->getPropertyNode($currentRunNode), $this->getPropertyNode($nextRunNode));
if ($isEqual === true) {
$nextValueNode = $this->getValueNode($nextRunNode);
$currentValueNode = $this->getValueNode($currentRunNode);
if ($nextValueNode !== null && $currentValueNode !== null) {
// fixme libreoffice docx quick fix
$appendTextNode = $this->document->createTextNode($nextValueNode->textContent);
$currentValueNode->appendChild($appendTextNode);
}
$clonedParagraphNode->removeChild($nextRunNode);
} else {
$currentRunNode = $nextRunNode;
}
// even if we remove element from document node list still contains it, so jump on next
$runIndex += 1;
$nextRunNode = $runNodeList->item($runIndex);
} else {
$currentRunNode = $nextRunNode;
}
}
$paragraphNode->parentNode->replaceChild($clonedParagraphNode, $paragraphNode);
}
// merge appended text nodes
$this->document->normalizeDocument();
}
示例11: renderDOM
public function renderDOM($json)
{
$page = new DOMDocument();
$page->normalizeDocument();
$page->formatOutput = true;
$html = $page->createElement('html');
$head = $page->createElement('head');
$title = $page->createElement('title');
$bootstrap = $page->createElement('link');
$body = $page->createElement('body');
$title_text = $page->createTextNode($json["title"]);
$title->appendChild($title_text);
$meta = $page->createElement('meta');
$meta->setAttribute("http-equiv", "Content-Type");
$meta->setAttribute("content", "text/html; charset=utf-8");
$meta = $head->appendChild($meta);
$bootstrap->setAttribute("rel", "stylesheet");
$bootstrap->setAttribute("href", $this->domain . "/assets/css/bootstrap/bootstrap.css");
$head->appendChild($title);
$head->appendChild($bootstrap);
$html->appendChild($head);
$html->appendChild($body);
$page->appendChild($html);
$countB = count($json["body"]);
for ($i = 0; $i < $countB; $i++) {
$tempEl = $page->createElement($json["body"][$i]["tag"]);
$countAttr = isset($json["body"][$i]["attribute"]) ? count($json["body"][$i]["attribute"]) : 0;
for ($j = 0; $j < $countAttr; $j++) {
$tempEl->setAttribute($json["body"][$i]["attribute"][$j][0], $json["body"][$i]["attribute"][$j][1]);
if ($json["body"][$i]["attribute"][$j][0] == "id") {
$tempEl->setIdAttribute($json["body"][$i]["attribute"][$j][0], true);
}
}
if (!$tempEl->hasAttribute("id")) {
$tempEl->setAttribute("id", $json["body"][$i]["tag"] . $body->getElementsByTagName($json["body"][$i]["tag"])->length);
}
if (isset($json["body"][$i]["text"])) {
$tempEl->appendChild($page->createTextNode($json["body"][$i]["text"]));
}
if (isset($json["body"][$i]["parent"])) {
$parent = $page->getElementById($json["body"][$i]["parent"]);
$parent->appendChild($tempEl);
} else {
$body->appendChild($tempEl);
}
}
$html->appendChild($head);
$html->appendChild($body);
$page->appendChild($html);
echo "<!DOCTYPE html>" . html_entity_decode($page->saveHTML());
}
示例12: __construct
public function __construct($document, $tagname = '*')
{
if (!is_scalar($document)) {
throw new Exception('Not a valid {JQMDoc} object');
}
$this->_namespace = microtime(true) . uniqid();
jqm_var($this->_namespace, $this);
//$document = preg_replace('/\s+/',' ',$document);
//Detect if $document is a valid full document
$hasHTML = stripos($document, '<html') !== false;
$this->__documentMap = array();
$DOM = new DOMDocument();
$DOM->recover = true;
$DOM->preserveWhiteSpace = true;
$DOM->substituteEntities = true;
$DOM->formatOutput = true;
$DOM->encoding = 'utf-8';
$DOM->loadHTML(mb_convert_encoding($document, 'HTML-ENTITIES', 'UTF-8'));
$DOM->normalizeDocument();
$html = $DOM->getElementsByTagName($tagname);
//Determine root / pieced map
$hasRoot = false;
if ($html->item(0)->childNodes->length > 0) {
$hasRoot = false;
$html_tmp = $html;
if ($html->item(0)->tagName == 'html') {
$html_tmp = $html->item(0)->childNodes->item(0);
}
if (!$hasHTML and $html_tmp->childNodes->length == 1) {
$hasRoot = true;
if ($html_tmp->childNodes->item(0)->firstChild) {
$root = $html_tmp->childNodes->item(0)->firstChild->getNodePath();
} else {
$root = $html_tmp->childNodes->item(0)->getNodePath();
}
}
}
$this->__schema['root'] = $hasRoot;
$this->__schema['rootPath'] = $root;
$this->__mapLength = false;
$this->_length = false;
$this->length = false;
if ($DOM->doctype) {
//$this->__schema['doctype'] = $DOM->saveHTML($DOM->doctype);
$DOM->removeChild($DOM->doctype);
}
//$output = $DOM->saveHTML();
//$this->__documentRaw = $output;
$this->_DOM = $DOM;
$this->_selector = $tagname;
}
示例13: compile
/**
* Compile document DOM to string view
* @param bool $a_format Format result
* @return string
*/
public function compile($a_format = false)
{
$d = parent::getData();
if ($d) {
return ZXmlEncoder::encode($d);
}
$this->m_document->preserveWhiteSpace = false;
$this->m_document->recover = false;
$f = $this->m_document->formatOutput;
$this->m_document->formatOutput = (bool) $a_format;
$this->m_document->normalizeDocument();
$return = $this->m_document->saveXML();
$this->m_document->formatOutput = $f;
return $return;
}
示例14: parseFile
/**
* Validates and parses the given file into a SimpleXMLElement
*
* @param string $file
* @return SimpleXMLElement
*/
protected function parseFile($file)
{
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
if (!$dom->load($file, LIBXML_COMPACT)) {
throw new \Exception(implode("\n", $this->getXmlErrors()));
}
if (!$dom->schemaValidate(__DIR__ . '/schema/dic/xliff-core/xliff-core-1.2-strict.xsd')) {
throw new \Exception(implode("\n", $this->getXmlErrors()));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
libxml_use_internal_errors(false);
return simplexml_import_dom($dom);
}
示例15: compile
/**
* Compile document DOM to string view
* @param bool $a_format Format result
* @return string
*/
public function compile($a_format = false)
{
$d = parent::getData();
if ($d) {
return Zoombi_Xml::encode($d);
}
$this->m_document->preserveWhiteSpace = $this->m_flags['whitespace'];
$this->m_document->recover = $this->m_flags['recover'];
$f = $this->m_document->formatOutput;
$this->m_document->formatOutput = $this->m_flags['format'];
if ($this->m_flags['normalize']) {
$this->m_document->normalizeDocument();
}
$return = $this->m_document->saveXML();
$this->m_document->formatOutput = $f;
return $return;
}