如果您想了解在通过DomDocument和PHP加载格式不正确的HTML时禁用警告的知识,那么本篇文章将是您的不二之选。我们将深入剖析在通过DomDocument的各个方面,并为您解答PHP加载格式不
如果您想了解在通过DomDocument和PHP加载格式不正确的HTML时禁用警告的知识,那么本篇文章将是您的不二之选。我们将深入剖析在通过DomDocument的各个方面,并为您解答PHP加载格式不正确的HTML时禁用警告的疑在这篇文章中,我们将为您介绍在通过DomDocument的相关知识,同时也会详细的解释PHP加载格式不正确的HTML时禁用警告的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 在通过DomDocument(PHP)加载格式不正确的HTML时禁用警告(在dom中通过对象name访问对象)
- delphi – 通过TXMLDocument访问IXMLDOMDocument2?
- document.write PHP XML操作类DOMDocument
- html5-tags上的PHP DOMDocument错误/警告
- PHP DomDocument HTML操作
在通过DomDocument(PHP)加载格式不正确的HTML时禁用警告(在dom中通过对象name访问对象)
我需要解析一些HTML文件,但是它们的格式不正确,PHP向其输出警告。我想以编程方式避免这种调试/警告行为。请指教。谢谢!
码:
// create a DOM document and load the HTML data$xmlDoc = new DomDocument;// this dumps out the warnings$xmlDoc->loadHTML($fetchResult);
这个:
@$xmlDoc->loadHTML($fetchResult)
可以禁止显示警告,但是如何以编程方式捕获这些警告?
答案1
小编典典您可以使用安装临时错误处理程序 set_error_handler
class ErrorTrap { protected $callback; protected $errors = array(); function __construct($callback) { $this->callback = $callback; } function call() { $result = null; set_error_handler(array($this, ''onError'')); try { $result = call_user_func_array($this->callback, func_get_args()); } catch (Exception $ex) { restore_error_handler(); throw $ex; } restore_error_handler(); return $result; } function onError($errno, $errstr, $errfile, $errline) { $this->errors[] = array($errno, $errstr, $errfile, $errline); } function ok() { return count($this->errors) === 0; } function errors() { return $this->errors; }}
用法:
// create a DOM document and load the HTML data$xmlDoc = new DomDocument();$caller = new ErrorTrap(array($xmlDoc, ''loadHTML''));// this doesn''t dump out any warnings$caller->call($fetchResult);if (!$caller->ok()) { var_dump($caller->errors());}
delphi – 通过TXMLDocument访问IXMLDOMDocument2?
但是,我需要启用XSLT Javascript函数(< msxml:script>标记)和 – 经过大量谷歌搜索 – 这意味着我需要将IXMLDOMDocument2的AllowXsltScript属性设置为true.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms760290(v=vs.85).aspx
我已经成功实现了这一点 – 但只能通过在msxmldom.pas中修改Delphi库函数CreateDOMDocument的源代码.
function CreateDOMDocument: IXMLDOMDocument; var doc :IXMLDOMDocument2; begin doc := TryObjectCreate([CLASS_DOMDocument60,CLASS_DOMDocument40,CLASS_DOMDocument30,CLASS_DOMDocument26,msxml.CLASS_DOMDocument]) as IXMLDOMDocument2; if not Assigned(doc) then raise DOMException.Create(SMSDOMnotinstalled); doc.setProperty('AllowXsltScript',true); // Allow XSLT scripts!! Result := doc; end;
显然这远非令人满意 – 所以如何在不修改库代码的情况下访问IXMLDOMDocument2对象?
解决方法
unit Unit27; interface uses xmldoc,xmlintf,msxml,msxmldom,Forms,SysUtils,ActiveX,ComObj,XmlDom,XmlConst,Windows,Messages,Classes,Controls,StdCtrls; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function TryObjectCreate(const GuidList: array of TGuid): IUnkNown; var I: Integer; Status: HResult; begin Status := S_OK; for I := Low(GuidList) to High(GuidList) do begin Status := CoCreateInstance(GuidList[I],nil,CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,Idispatch,Result); if Status = S_OK then Exit; end; OleCheck(Status); end; function CreateDOMDocument2: IXMLDOMDocument; var Doc2 : IXMLDOMDocument2; begin Doc2 := TryObjectCreate([CLASS_DOMDocument60,msxml.CLASS_DOMDocument]) as IXMLDOMDocument2; if not Assigned(Doc2) then raise DOMException.Create(SMSDOMnotinstalled); Doc2.setProperty('AllowXsltScript',true); Result := Doc2; end; procedure TForm1.FormCreate(Sender: TObject); var Doc : IXMLDocument; begin Doc := TXMLDocument.Create(nil); Doc.LoadFromFile('c:\temp\test.xml'); end; initialization MSXMLDOMDocumentCreate := CreateDOMDocument2; end.
document.write PHP XML操作类DOMDocument
html5-tags上的PHP DOMDocument错误/警告
我一直在尝试解析HTML5代码,以便可以在代码内设置属性/值,但是DOMDocument(PHP5.3)似乎不支持诸如<nav>
和的标记<section>
。
有什么方法可以在PHP中将其解析为HTML并处理代码?
复制代码:
<?php
$dom = new DOMDocument();
$dom->loadHTML("<!DOCTYPE HTML>
<html><head><title>test</title></head>
<body>
<nav>
<ul>
<li>first
<li>second
</ul>
</nav>
<section>
...
</section>
</body>
</html>");
错误
警告:DOMDocument :: loadHTML():在实体中的标签导航无效,第17行在/home/wbkrnl/public_html/new-
mvc/1.php中的第4行警告:DOMDocument :: loadHTML():标记部分在实体中无效,第17行在/home/wbkrnl/public_html/new-
mvc/1.php中的第10行
PHP DomDocument HTML操作
我有以下HTML.
<div id="container">
<div id="current">Current Div</div>
</div>
我在PHP中使用DomDocument,试图在id为“ current”的div之前向HTML添加一个额外的div.
<div id="container">
<div id="new">New Div</div>
<div id="current">Current Div</div>
</div>
当我使用以下代码时,似乎在div内添加了div,其ID为“ current”,但在该div的内容之前.有人可以告诉我这是为什么,以及如何获得类似于上述HTML的结果? (请参见下面的HTML问题)
当前的PHP
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTMLFile('index.html');
$head = $doc->getElementById('current');
$base = $doc->createElement('div', 'New Div');
$base->setAttribute('id', 'new');
echo $doc->saveHTML();
HTML问题
<div id="container">
<div id="current">
<div id="new">New Div</div>
Current Div
</div>
</div>
编辑:
的PHP
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTMLFile('index.html');
$container = $doc->getElementById('container');
$current = $doc->getElementById('username');
$new = $doc->createElement('div', 'New Div');
$new->setAttribute('id', 'new');
$container->insertBefore($new, $current);
echo $doc->saveHTML();
的HTML
<div id="container">
<form method="post" action="">
<input type="text" name="username" id="username" />
</form>
</div>
错误:
Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error'
in index.PHP:55 Stack trace: #0 index.PHP(55):
DOMNode->insertBefore(Object(DOMElement), Object(DOMElement))
解决方法:
您可以使用DOMNode::insertBefore()
:
<?PHP
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTMLFile('index.html');
$container = $doc->getElementById('container');
$current = $doc->getElementById('current');
$new = $doc->createElement('div', 'New Div');
$new->setAttribute('id', 'new');
$container->insertBefore($new, $current);
var_dump($doc->saveHTML());
这样产生:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<div id="container">
<div id="new">New Div</div>
<div id="current">Current Div</div>
</div>
</body>
</html>
希望这可以帮助 :)
编辑
关于您报告的问题…我看到了问题:input#username不是div#container的直接子代,它是form的直接子代.因此,您需要将form元素作为DOMNode对象而不是其父div#container来获取.最简单的方法是在表单中添加一个ID,然后执行以下操作:
$form = $doc->getElementById('form');
$username = $doc->getElementById('username');
$username->setAttribute('value', 'Enter username!');
var_dump($doc->saveHTML());
这是基于以下HTML.请注意,表单元素具有一个ID:
<div id="container">
<form id="form" method="post" action="">
<input type="text" name="username" id="username">
</form>
</div>
如果由于某种原因无法将id添加到form元素,则可以像这样从div#container遍历:
// find div#container element
$container = $doc->getElementBy('id');
// find all form elements that are children of div#container
$forms = $container->getElementsByTagName('form');
if (0 !== $forms->length) {
$form = $forms->item(0);
}
// etc.
编辑#2
我差点忘了XPath …如果您想变得更简洁/喜欢冒险,可以使用DOMXPath::query()
查找DOM节点:
$xpath = new DOMXPath($doc);
$form = $xpath->query('body/div[@id="container"]/form')->item(0);
$input = $xpath->query('body//input[@id="username"]')->item(0);
XPath查询语法非常隐秘但功能强大,我不能以任何方式说自己是专家,所以我不能说这些查询的效率.另外,您可能想添加错误检查-查询方法返回具有length属性的DOMNodeList.
最后,值得注意的是DOMDocument用DOCTYPE和html和body标签装饰HTML片段.这就是XPath查询从正文遍历的原因.
关于在通过DomDocument和PHP加载格式不正确的HTML时禁用警告的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于delphi – 通过TXMLDocument访问IXMLDOMDocument2?、document.write PHP XML操作类DOMDocument、html5-tags上的PHP DOMDocument错误/警告、PHP DomDocument HTML操作的相关知识,请在本站寻找。
本文标签: