2011-09-12 11 views
0

DOMDocumentへのアクセスを許可しない単純なHTML文字列(HTML-tidyに関係なく)をロードしようとしています。ここでPHP DOMDocumentは常に空のオブジェクトを生成します

私が取得インスタンス化

$doc = new DOMDocument(/*'1.0', 'utf-8'*/); 
    $doc->recover = true; 
    $doc->strictErrorChecking = false; 
    $doc->formatOutput = true; 
    $doc->load($content); 

    $node_array = $doc->getElementsByTagName("body"); 
    print_r($node_array) 

...または$node_array->items(0);

です:

DOMNodeList Object 
(
) 

のDOMDocumentは、それがリソースではありません 保存機能を持つだけで罰金文字列を返します。それは、依存関係、追加のPHP構成...が欠けている可能性がありますか?

更新:のDOMDocumentのオブジェクトは、単に任意ののtoString変換機能が実装されていない:クラスのDOMNodeListの

print_r((string)$node_array); 

オブジェクトがで....


文字列に変換することができませんでした

HTMLコードはここにあります: http://pastebin.com/11V92Dup(意図的に不正な形式 - これはコードで、「きちんとした」がタグを適切に閉じていることを証明することです)

私は単にノードと出力その内容を歩きたい:

$node_array = $doc->getElementsByTagName("html");//parent_node(); 
    $x = $doc->documentElement; 
    foreach ($x->childNodes AS $item) 
     { 
     print $item->nodeName . " = " . $item->nodeValue . "<br />"; 
     } 

UPDATE 2:私はこの結果を得ます!それは意味をなさない。 (すべての空白はどこから来るのですか?)

body = 







        COMPOUND: C05441 
+0

あなたのHTML文字列はどこですか? – ajreal

+0

申し訳ありませんが、まさに質問ですか?あなたはすべての体を文字列として取得したいですか?これが真で、DOMDocumentでこれを行う場合は、最初のノードをクローンして新しいDOMDocumentに挿入する必要があります。このように - $ node_arr = $ doc-> getElemenetsByTagName( 'body'); if($ node_arr-> length){$ new_dom =新しいDOMDocument; $ new_dom-> appendChild($ node_arr-items(0) - > cloneNode(true))}を実行します。しかし、sunstring/strposやregexpをもっとうまく使うための助言 – ZigZag

+0

bodyタグの下のHTMLタグによって空白が生じています。何を探していますか? – ajreal

答えて

0

私はあなたが答えを期待しているかについてはっきりしていません。とにかく試してみるよ。 HTMLツリーを再帰的に繰り返し、各要素のtextContent値を出力するコードです。

<?php 

$contents = <<<HTML 
<html><head> 
<title>KEGG COMPOUND: C05441</title> 
<link type="text/css" rel="stylesheet" href="/css/gn2.css"> 
<link rel="stylesheet" href="/css/bget.css" type="text/css"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<meta http-equiv="Content-Script-Type" content="text/javascript"> 
</head> 
<body onload="window.focus();init();" bgcolor="#ffffff"> 
<table border=0 cellpadding=0 cellspacing=0><tr><td> 
<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="70"><a href="/kegg/kegg2.html"><img align="middle" border="0" src="/Fig/bget/kegg2.gif" alt="KEGG"></a></td><td>&nbsp;&nbsp;&nbsp;</td><td><a name="compound:C05441"></a><font class="title2">COMPOUND: C05441</font></td><td align="right" valign="bottom"><a href="javascript:void(window.open('/kegg/document/help_bget_compound.html','KEGG_Help','toolbar=no,location=no,directories=no,width=720,height=640,resizable=yes,scrollbars=yes'))"><img onmouseup="btn(this,'Hb')" align="middle" onmouseout="btn(this,'Hb')" onmousedown="btn(this,'Hbd')" onmouseover="btn(this,'Hbh')" alt="Help" name="help" border="0" src="/Fig/bget/button_Hb.gif"></a></td></tr></table> 
<form method="post" action="/dbget-bin/www_bget" enctype="application/x-www-form-urlencoded" name="form1"> 
<table border=0 cellpadding=1 cellspacing=0> 
<tr> 
<td class="fr2"> 
<table border=0 cellpadding=2 cellspacing=0 style="border-bottom:#000 1px solid"> 

</table> 
</body></html> 
HTML; 

$doc = new DOMDocument("1.0", "UTF-8"); 
$doc->loadHTML($contents); 

header("Content-Type: text/plain; charset=utf-8"); 

function recursivelyEchoChildNodes (DOMElement $parent, $depth = 1) { 
    foreach ($parent->childNodes as $node) { 
     if ($node instanceof DOMElement) { 
      echo str_repeat("-", $depth) . " " . $node->localName . " = " . $node->textContent . "\n"; 
      if ($node->hasChildNodes()) { 
       recursivelyEchoChildNodes($node, $depth + 1); 
      } 
     } 
    } 
} 

$html = $doc->getElementsByTagName("html")->item(0); 
recursivelyEchoChildNodes($html); 
関連する問題