Document Object Model
- はじめに
- インストール/設定
- 定義済み定数
- 例
- DOMAttr — DOMAttr クラス
- DOMAttr::__construct — 新しい DOMAttr オブジェクトを作成する
- DOMAttr::isId — 属性が定義済みの ID かどうかを調べる
- DOMCdataSection — DOMCdataSection クラス
- DOMCdataSection::__construct — 新しい DOMCdataSection オブジェクトを作成する
- DOMCharacterData — DOMCharacterData クラス
- DOMCharacterData::after — 文字データの後ろにノードを追加する
- DOMCharacterData::appendData — ノードの文字データの最後に文字列を追加する
- DOMCharacterData::before — ノードの前にノードを追加する
- DOMCharacterData::deleteData — 指定した範囲の文字列をノードから削除する
- DOMCharacterData::insertData — 指定した 16 ビット単位のオフセットに、文字列を挿入する
- DOMCharacterData::remove — 文字データを削除する
- DOMCharacterData::replaceData — DOMCharacterData ノードの文字列の一部を置換する
- DOMCharacterData::replaceWith — 文字データを新しいノードで置き換える
- DOMCharacterData::substringData — ノードから指定した範囲のデータを抽出する
- DOMChildNode — DOMChildNode インターフェイス
- DOMChildNode::after — ノードの後ろにノードを追加する
- DOMChildNode::before — ノードの前にノードを追加する
- DOMChildNode::remove — ノードを削除する
- DOMChildNode::replaceWith — ノードを新しいノードで置き換える
- DOMComment — DOMComment クラス
- DOMComment::__construct — 新しい DOMComment オブジェクトを作成する
- DOMDocument — DOMDocument クラス
- DOMDocument::adoptNode — ノードを別のドキュメントに移す
- DOMDocument::append — 最後の子ノードの後ろにノードを追加する
- DOMDocument::__construct — 新しい DOMDocument オブジェクトを作成する
- DOMDocument::createAttribute — 新しい属性を作成する
- DOMDocument::createAttributeNS — 関連付けられた名前空間に新しい属性を作成する
- DOMDocument::createCDATASection — 新しい cdata ノードを作成する
- DOMDocument::createComment — 新しい comment ノードを作成する
- DOMDocument::createDocumentFragment — 新しい文書片を作成する
- DOMDocument::createElement — 新しい要素ノードを作成する
- DOMDocument::createElementNS — 関連付けられた名前空間に新しい要素を作成する
- DOMDocument::createEntityReference — 新しいエンティティ参照ノードを作成する
- DOMDocument::createProcessingInstruction — 新しい PI ノードを作成する
- DOMDocument::createTextNode — 新しいテキストノードを作成する
- DOMDocument::getElementById — id に対応する要素を検索する
- DOMDocument::getElementsByTagName — 指定したローカルタグ名に対応するすべての要素を検索する
- DOMDocument::getElementsByTagNameNS — 指定した名前空間で、タグ名に対応するすべての要素を検索する
- DOMDocument::importNode — 現在のドキュメントにノードをインポートする
- DOMDocument::load — ファイルから XML を読み込む
- DOMDocument::loadHTML — 文字列から HTML を読み込む
- DOMDocument::loadHTMLFile — ファイルから HTML を読み込む
- DOMDocument::loadXML — 文字列から XML を読み込む
- DOMDocument::normalizeDocument — ドキュメントを正規化する
- DOMDocument::prepend — 最初の子ノードの前にノードを追加する
- DOMDocument::registerNodeClass — 基底ノード型を作成する際に使用する拡張クラスを登録する
- DOMDocument::relaxNGValidate — ドキュメントを relaxNG で検証する
- DOMDocument::relaxNGValidateSource — ドキュメントを relaxNG で検証する
- DOMDocument::replaceChildren — ドキュメントの子を置換する
- DOMDocument::save — 内部の XML ツリーをファイルに出力する
- DOMDocument::saveHTML — 内部のドキュメントを HTML 形式の文字列として出力する
- DOMDocument::saveHTMLFile — 内部のドキュメントを HTML 形式でファイルに出力する
- DOMDocument::saveXML — 内部の XML ツリーを文字列として出力する
- DOMDocument::schemaValidate — スキーマに基づいてドキュメントを検証する。XML Schema 1.0 のみサポート。
- DOMDocument::schemaValidateSource — スキーマに基づいてドキュメントを検証する
- DOMDocument::validate — DTD に基づいてドキュメントを検証する
- DOMDocument::xinclude — DOMDocument オブジェクト内の XIncludes を置換する
- DOMDocumentFragment — DOMDocumentFragment クラス
- DOMDocumentFragment::append — 最後の子ノードの後ろにノードを追加する
- DOMDocumentFragment::appendXML — 生の XML データを追加する
- DOMDocumentFragment::__construct — DOMDocumentFragment オブジェクトを構築する
- DOMDocumentFragment::prepend — 最初の子ノードの前にノードを追加する
- DOMDocumentFragment::replaceChildren — 文書片の子を置換する
- DOMDocumentType — DOMDocumentType クラス
- DOMElement — DOMElement クラス
- DOMElement::after — 要素の後ろにノードを追加する
- DOMElement::append — 最後のノードの後ろにノードを追加する
- DOMElement::before — 要素の前にノードを追加する
- DOMElement::__construct — 新しい DOMElement オブジェクトを作成する
- DOMElement::getAttribute — 属性の値を返す
- DOMElement::getAttributeNames — 属性の名前を取得する
- DOMElement::getAttributeNode — 属性ノードを返す
- DOMElement::getAttributeNodeNS — 属性ノードを返す
- DOMElement::getAttributeNS — 属性の値を返す
- DOMElement::getElementsByTagName — タグ名から要素を取得する
- DOMElement::getElementsByTagNameNS — 名前空間 URI とローカル名から要素を取得する
- DOMElement::hasAttribute — 属性が存在するかどうかを調べる
- DOMElement::hasAttributeNS — 属性が存在するかどうかを調べる
- DOMElement::insertAdjacentElement — 隣接する要素を挿入する
- DOMElement::insertAdjacentText — 隣接するテキストを挿入する
- DOMElement::prepend — 最初の子ノードの前にノードを追加する
- DOMElement::remove — 要素を削除する
- DOMElement::removeAttribute — 属性を削除する
- DOMElement::removeAttributeNode — 属性を削除する
- DOMElement::removeAttributeNS — 属性を削除する
- DOMElement::replaceChildren — 要素中の子を置換する
- DOMElement::replaceWith — 要素を新しいノードで置換する
- DOMElement::setAttribute — 属性を新しく追加する、または変更する
- DOMElement::setAttributeNode — 新しい属性ノードを要素に追加する
- DOMElement::setAttributeNodeNS — 新しい属性ノードを要素に追加する
- DOMElement::setAttributeNS — 新しい属性を追加する
- DOMElement::setIdAttribute — ID 型の属性を名前で宣言する
- DOMElement::setIdAttributeNode — ID 型の属性をノードで宣言する
- DOMElement::setIdAttributeNS — ID 型の属性をローカル名および名前空間 URI で宣言する
- DOMElement::toggleAttribute — 属性の値を切り替える
- DOMEntity — DOMEntity クラス
- DOMEntityReference — DOMEntityReference クラス
- DOMEntityReference::__construct — 新しい DOMEntityReference オブジェクトを作成する
- DOMException — DOMException クラス
- DOMImplementation — DOMImplementation クラス
- DOMImplementation::__construct — 新しい DOMImplementation オブジェクトを作成する
- DOMImplementation::createDocument — 指定した型とドキュメント要素の DOMDocument オブジェクトを作成する
- DOMImplementation::createDocumentType — 空の DOMDocumentType オブジェクトを作成する
- DOMImplementation::hasFeature — DOM 実装が、指定した機能を実装しているかどうかを調べる
- DOMNamedNodeMap — DOMNamedNodeMap クラス
- DOMNamedNodeMap::count — マップにあるノードの数を取得する
- DOMNamedNodeMap::getIterator — 外部のイテレータを取得する
- DOMNamedNodeMap::getNamedItem — 名前で指定されたノードを取得する
- DOMNamedNodeMap::getNamedItemNS — ローカル名および名前空間 URI で指定したノードを取得する
- DOMNamedNodeMap::item — インデックスで指定したノードを取得する
- DOMNameSpaceNode — DOMNameSpaceNode クラス
- DOMNode — DOMNode クラス
- DOMNode::appendChild — 子要素群の最後に新しい子要素を追加する
- DOMNode::C14N — ノードを文字列に正規化する
- DOMNode::C14NFile — ノードをファイルに正規化する
- DOMNode::cloneNode — ノードを複製する
- DOMNode::contains — ノードが別のノードを含んでいるかを調べる
- DOMNode::getLineNo — ノードが存在する行の番号を取得します。
- DOMNode::getNodePath — ノードの XPath を取得する
- DOMNode::getRootNode — ルート(根)ノードを取得する
- DOMNode::hasAttributes — ノードが属性を保持しているかどうかを調べる
- DOMNode::hasChildNodes — ノードが子を保持しているかどうかを調べる
- DOMNode::insertBefore — 参照しているノードの前に新しい子を追加する
- DOMNode::isDefaultNamespace — 指定した namespaceURI がデフォルトの名前空間かどうかを調べる
- DOMNode::isEqualNode — ノードが等しいかを調べる
- DOMNode::isSameNode — 2 つのノードが等しいかどうかを調べる
- DOMNode::isSupported — 指定したバージョンで機能がサポートされているかどうかを調べる
- DOMNode::lookupNamespaceURI — プレフィックスに基づいて、ノードの名前空間 URI を取得する
- DOMNode::lookupPrefix — 名前空間 URI に基づいて、ノードの名前空間プレフィックスを取得する
- DOMNode::normalize — ノードを正規化する
- DOMNode::removeChild — 子要素群から子要素を削除する
- DOMNode::replaceChild — 子を置き換える
- DOMNodeList — DOMNodeList クラス
- DOMNodeList::count — リストにあるノードの数を取得する
- DOMNodeList::getIterator — 外部のイテレータを取得する
- DOMNodelist::item — インデックスで指定したノードを取得する
- DOMNotation — DOMNotation クラス
- DOMParentNode — DOMParentNode インターフェイス
- DOMParentNode::append — 最後の子ノードの後ろにノードを追加する
- DOMParentNode::prepend — 最初の子ノードの前にノードを追加する
- DOMParentNode::replaceChildren — ノード中の子を置換する
- DOMProcessingInstruction — DOMProcessingInstruction クラス
- DOMProcessingInstruction::__construct — 新しい DOMProcessingInstruction オブジェクトを作成する
- DOMText — DOMText クラス
- DOMText::__construct — 新しい DOMText オブジェクトを作成する
- DOMText::isElementContentWhitespace — このテキストノードが、要素の内容にホワイトスペースが含まれているかを返す
- DOMText::isWhitespaceInElementContent — このテキストノードが空白を含むかどうかを示す
- DOMText::splitText — 指定したオフセットでノードを 2 つに分割する
- DOMXPath — DOMXPath クラス
- DOMXPath::__construct — 新しい DOMXPath オブジェクトを作成する
- DOMXPath::evaluate — 与えられた XPath 式を評価し、可能であれば結果を返す
- DOMXPath::query — 与えられた XPath 式を評価する
- DOMXPath::registerNamespace — DOMXPath オブジェクトの名前空間を登録する
- DOMXPath::registerPhpFunctions — PHP の関数を XPath 関数として登録する
- DOM 関数
- dom_import_simplexml — SimpleXMLElement オブジェクトから DOMElement オブジェクトを取得する
+add a note
User Contributed Notes 38 notes
tobiasz.cudnik[at]gmail.com ¶
16 years ago
If you need simple interface to DOM check out phpQuery - jQuery port to PHP:
http://code.google.com/p/phpquery/
It uses CSS selectors to fetch nodes.
Here's example how it works:
<?php
// just one file to include
require('phpQuery/phpQuery.php');
$html = '
<div>
mydiv
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>';
// intialize new DOM from markup
phpQuery::newDocument($markup)
->find('ul > li')
->addClass('my-new-class')
->filter(':last')
->addClass('last-li');
// query all unordered lists in last used DOM
pq('ul')->insertAfter('div');
// iterate all LIs from last used DOM
foreach(pq('li') as $li) {
// iteration returns plain DOM nodes, not phpQuery objects
pq($li)->addClass('my-second-new-class');
}
// same as pq('anything')->htmlOuter()
// but on document root (returns doctype etc)
print phpQuery::getDocument();
?>
It uses DOM extension and XPath so it works only in PHP5.
Yanik <clonyara(at)ahoo(dot)com> ¶
17 years ago
I hate DOM model !
so I wrote dom2array simple function (simple for use):
function dom2array($node) {
$res = array();
print $node->nodeType.'<br/>';
if($node->nodeType == XML_TEXT_NODE){
$res = $node->nodeValue;
}
else{
if($node->hasAttributes()){
$attributes = $node->attributes;
if(!is_null($attributes)){
$res['@attributes'] = array();
foreach ($attributes as $index=>$attr) {
$res['@attributes'][$attr->name] = $attr->value;
}
}
}
if($node->hasChildNodes()){
$children = $node->childNodes;
for($i=0;$i<$children->length;$i++){
$child = $children->item($i);
$res[$child->nodeName] = dom2array($child);
}
}
}
return $res;
}
super dot puma at gmail dot com ¶
10 years ago
If you want to print the DOM XML file content, you can use the next code:
$doc = new DOMDocument();
$doc->load($xmlFileName);
echo "<br>" . $doc->documentURI;
$x = $doc->documentElement;
getNodeContent($x->childNodes, 0);
function getNodeContent($nodes, $level){
foreach ($nodes AS $item) {
// print "<br><br>TIPO: " . $item->nodeType ;
printValues($item, $level);
if ($item->nodeType == 1) { //DOMElement
foreach ($item->attributes AS $itemAtt) {
printValues($itemAtt, $level+3);
}
if($item->childNodes || $item->childNodes->lenth > 0) {
getNodeContent($item->childNodes, $level+5);
}
}
}
}
function printValues($item, $level){
if ($item->nodeType == 1) { //DOMElement
printLevel($level);
print $item->nodeName . " = " . $item->nodeValue;
}
if ($item->nodeType == 2) { //DOMAttr
printLevel($level);
print $item->name . " = " . $item->value ;
}
if ($item->nodeType == 3) { //DOMText
if ($item->isWhitespaceInElementContent() == false){
printLevel($level);
print $item->wholeText ;
}
}
}
function printLevel($level)
{
print "<br>";
if ($level == 0) {
print "<br>";
}
for($i=0; $i < $level; $i++) {
print "-";
}
}
pes_cz ¶
19 years ago
When I tried to parse my XHTML Strict files with DOM extension, it couldn't understand xhtml entities (like ©). I found post about it here (14-Jul-2005 09:05) which adviced to add resolveExternals = true, but it was very slow. There was some small note about xml catalogs but without any glue. Here it is:
XML catalogs is something like cache. Download all needed dtd's to /etc/xml, edit file /etc/xml/catalog and add this line: <public publicId="-//W3C//DTD XHTML 1.0 Strict//EN" uri="file:///etc/xml/xhtml1-strict.dtd" />
Thats all. Thanks to http://www.whump.com/moreLikeThis/link/03815
simlee at indiana dot edu ¶
18 years ago
The project I'm currently working on uses XPaths to dynamically navigate through chunks of an XML file. I couldn't find any PHP code on the net that would build the XPath to a node for me, so I wrote my own function. Turns out it wasn't as hard as I thought it might be (yay recursion), though it does entail using some PHP shenanigans...
Hopefully it'll save someone else the trouble of reinventing this wheel.
<?php
function getNodeXPath( $node ) {
// REMEMBER THAT XPATHS USE BASE-1 INSTEAD OF BASE-0!!!
// Get the index for the current node by looping through the siblings.
$parentNode = $node->parentNode;
if( $parentNode != null ) {
$nodeIndex = 0;
do {
$testNode = $parentNode->childNodes->item( $nodeIndex );
$nodeName = $testNode->nodeName;
$nodeIndex++;
// PHP trickery! Here we create a counter based on the node
// name of the test node to use in the XPath.
if( !isset( $$nodeName ) ) $$nodeName = 1;
else $$nodeName++;
// Failsafe return value.
if( $nodeIndex > $parentNode->childNodes->length ) return( "/" );
} while( !$node->isSameNode( $testNode ) );
// Recursively get the XPath for the parent.
return( getNodeXPath( $parentNode ) . "/{$node->nodeName}[{$$nodeName}]" );
} else {
// Hit the root node! Note that the slash is added when
// building the XPath, so we return just an empty string.
return( "" );
}
}
?>
johanwthijs-at-hotmail-dot-com ¶
18 years ago
Being an experienced ASP developer I was wondering how to replace textual content of a node (with msxml this is simply acheived by setting the 'text' property of a node). Out of frustration I started to play around with SimpleXml but I could not get it to work in combination with xPath.
I took me a lot of time to find out so I hope this helps others:
function replaceNodeText($objXml, $objNode, $strNewContent){
/*
This function replaces a node's string content with strNewContent
*/
$objNodeListNested = &$objNode->childNodes;
foreach ( $objNodeListNested as $objNodeNested ){
if ($objNodeNested->nodeType == XML_TEXT_NODE)$objNode->removeChild ($objNodeNested);
}
$objNode->appendChild($objXml->createTextNode($strNewContent));
}
$objXml= new DOMDocument();
$objXml->loadXML('<root><node id="1">bla</note></root>');
$objXpath = new domxpath($objXml);
$strXpath="/root/node[@id='1']";
$objNodeList = $objXpath ->query($strXpath);
foreach ($objNodeList as $objNode){
//pass the node by reference
replaceNodeText($objXml, &$objNode, $strImportedValue);
}
cooper at asu dot ntu-kpi dot kiev dot ua ¶
17 years ago
If you are using not object-oriented functions and it takes too much time to change them all (or you'll be replacing them later) then as a temporary decision can be used this modules:
For DOM XML:
http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
For XSLT:
http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
aidan at php dot net ¶
19 years ago
When dealing with validation or loading, the output errors can be quite annoying.
PHP 5.1 introduces libxml_get_errors().
http://php.net/libxml_get_errors
aidan at php dot net ¶
19 years ago
As of PHP 5.1, libxml options may be set using constants rather than the use of proprietary DomDocument properties.
DomDocument->resolveExternals is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDATTR
DomDocument->validateOnParse is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDVALID
PHP 5.1 users are encouraged to use the new constants.
Example:
DomDocument->load($file, LIBXML_DTDLOAD|LIBXML_DTDATTR);
DomDocument->load($file, LIBXML_DTDLOAD|LIBXML_DTDVALID);
toby at tobiasly dot com ¶
18 years ago
This module is not included by default either in the CentOS 4 "centosplus" repository. For those using PHP5 on CentOS 4, a simple "yum --enablerepo=centosplus install php-xml" will do the trick (this will install both the XML and DOM modules).
sweisman at pobox dot com ¶
15 years ago
I had problems with the dom2array_full function by "nospam at ya dot ru". Here's my function, which works correctly for my project, and might work for yours:
<?php
function dom_to_array($root)
{
$result = array();
if ($root->hasAttributes())
{
$attrs = $root->attributes;
foreach ($attrs as $i => $attr)
$result[$attr->name] = $attr->value;
}
$children = $root->childNodes;
if ($children->length == 1)
{
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE)
{
$result['_value'] = $child->nodeValue;
if (count($result) == 1)
return $result['_value'];
else
return $result;
}
}
$group = array();
for($i = 0; $i < $children->length; $i++)
{
$child = $children->item($i);
if (!isset($result[$child->nodeName]))
$result[$child->nodeName] = dom_to_array($child);
else
{
if (!isset($group[$child->nodeName]))
{
$tmp = $result[$child->nodeName];
$result[$child->nodeName] = array($tmp);
$group[$child->nodeName] = 1;
}
$result[$child->nodeName][] = dom_to_array($child);
}
}
return $result;
}
?>
PHPdeveloper ¶
17 years ago
The Yanik's dom2array() function (added on 14-Mar-2007 08:40) does not handle multiple nodes with the same name, i.e.:
<foo>
<name>aa</name>
<name>bb</name>
</foo>
It will overwrite former and your array will contain just the last one ("bb")
amir.laherATcomplinet.com ¶
19 years ago
This particular W3C page provides invaluable documentation for the DOM classes implemented in php5 (via libxml2). It fills in plenty of php.net's gaps:
http://www.w3.org/TR/DOM-Level-2-Core/core.html
Some key examples:
* concise summary of the class heirachy (1.1.1)
* clarification that DOM level 2 doesn't allow for population of internal DTDs
* explanation of DOMNode->normalize()
* explanation of the DOMImplementation class
The interfaces are described in OMG's Interface Definition Language
ohcc at 163 dot com ¶
8 years ago
<?php
// this note is about how to get a DOMNode's outerHTML and innerHTML.
$dom = new DOMDocument('1.0','UTF-8');
$dom->loadHTML('<html><body><div><p>p1</p><p>p2</p></div></body></html>');
$node = $dom->getElementsByTagName('div')->item(0);
$outerHTML = $node->ownerDocument->saveHTML($node);
$innerHTML = '';
foreach ($node->childNodes as $childNode){
$innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
}
echo '<h2>outerHTML: </h2>';
echo htmlspecialchars($outerHTML);
echo '<h2>innerHTML: </h2>';
echo htmlspecialchars($innerHTML);
?>
miguelangelhdz at NOSPAM dot com ¶
16 years ago
After searching how to extend the DOMDocument and DOMElement I found a way in the bug: http://bugs.php.net/bug.php?id=35104. The following code shows how:
<?php
class extDOMDocument extends DOMDocument {
public function createElement($name, $value=null) {
$orphan = new extDOMElement($name, $value); // new sub-class object
$docFragment = $this->createDocumentFragment(); // lightweight container maintains "ownerDocument"
$docFragment->appendChild($orphan); // attach
$ret = $docFragment->removeChild($orphan); // remove
return $ret; // ownerDocument set; won't be destroyed on method exit
}
// .. more class definition
}
class extDOMElement extends DOMElement {
function __construct($name, $value='', $namespaceURI=null) {
parent::__construct($name, $value, $namespaceURI);
}
// ... more class definition here
}
$doc = new extDOMDocument('test');
$el = $doc->createElement('tagname');
$el->setAttribute("attr", "val");
$doc->appendChild($el);
// append discards the DOMDocumentFragment and just adds its child nodes, but ownerDocument is maintained.
echo get_class($el)."<br/>";
echo get_class($doc->documentElement)."<br/>";
echo "<xmp>".$doc->saveXML()."</xmp>";
?>
emmanuellutula at hotmail dot com ¶
6 years ago
If you want to use DOMDocument in your PHPUnit Tests drive on Symfony Controller (testing form)! Use like :
namespace Tests\YourBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use YourBundle\Controller\TextController;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient(array(), array());
$crawler = $client->request('GET', '/text/add');
$this->assertTrue($crawler->filter("form")->count() > 0, "Text form exist !");
$form = $crawler->filter("form")->form();
$domDocument = new \DOMDocument;
$domInput = $domDocument->createElement('input');
$dom = $domDocument->appendChild($domInput);
$dom->setAttribute('slug', 'bloc');
$formInput = new \Symfony\Component\DomCrawler\Field\InputFormField($domInput);
$form->set($formInput);
$crawler = $client->submit($form);
if ($client->getResponse()->isRedirect())
{
$crawler = $client->followRedirect(false);
}
// $this->assertTrue($client->getResponse()->isSuccessful());
//$this->assertEquals(200, $client->getResponse()->getStatusCode(),
// "Unexpected HTTP status code for GET /backoffice/login");
}
}
Drupella ¶
13 years ago
Here is a fast innerHTML function that returns the result without iterating over child nodes.
<?php
function innerHTML($el) {
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($el, TRUE));
$html = trim($doc->saveHTML());
$tag = $el->nodeName;
return preg_replace('@^<' . $tag . '[^>]*>|</' . $tag . '>$@', '', $html);
}
?>
Example
<?php
$doc = new DOMDocument();
// A corrupt HTML string
$doc->loadHTML('<HTML><A HREF="ss">asd</A>');
$body = $doc->getElementsByTagName('body')->item(0);
print htmlspecialchars(innerHTML($body));
// Prints <a href="ss">asd</a>
?>
odessa131 at aol dot nospam dot com ¶
15 years ago
I had the hardest time updating a complex XML document. Here's a quick example on how to do it.
<?php
// Load the XML from a file.
$xml = "a2062.xml"; // This is an XFDL form previously unencoded and ungzipped.
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->Load($xml);
// Create an XPath query.
// Note: you must define the namespace if the XML document has defined namespaces.
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('xfdl', "http://www.PureEdge.com/XFDL/6.5");
// Locate the value for the first Item Description field.
$query = "//xfdl:page/xfdl:field[@sid='ITEMDESA']/xfdl:value";
$nodeList = $xpath->query($query);
$nodeList->item(0)->nodeValue = "This is the text in the value node of the first Item Description field inside the DA 2062 PureEdge form.";
$dom->save($xml);
?>
I hope this helps someone.
Junior ¶
15 years ago
innerHTML in PHP DOM
<?php
function DOMinnerHTML($element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
?>
Example:
<?php
$dom= new DOMDocument();
$dom->load($html_string);
$dom->preserveWhiteSpace = false;
$domTable = $dom->getElementsByTagName("table");
foreach ($domTable as $tables)
{
echo DOMinnerHTML($tables);
}
?>
fantasyman3000 at gmail dot com ¶
15 years ago
In response to "simlee at indiana dot edu",
- First of all thanks for sharing your funciton.
- It didn't work for me so i rewrite it from scratch using different method.
Here is the new version, hope it helps someone :
<?php
/**
* result sample : /html[1]/body[1]/span[1]/fieldset[1]/div[1]
* @return string
*/
function getNodeXPath( $node ) {
$result='';
while ($parentNode = $node->parentNode) {
$nodeIndex=-1;
$nodeTagIndex=0;
do {
$nodeIndex++;
$testNode = $parentNode->childNodes->item( $nodeIndex );
if ($testNode->nodeName==$node->nodeName and $testNode->parentNode->isSameNode($node->parentNode) and $testNode->childNodes->length>0) {
//echo "{$testNode->parentNode->nodeName}-{$testNode->nodeName}-{}<br/>";
$nodeTagIndex++;
}
} while (!$node->isSameNode($testNode));
$result="/{$node->nodeName}[{$nodeTagIndex}]".$result;
$node=$parentNode;
};
return $result;
}
?>
By Sina.Salek.ws
ben_demott at hotmail dot com ¶
14 years ago
A function among several others to parse a google results page, I wrote this some time ago - google has probably changed their site since then, but I thought this might be helpful to someone.
I'm moving servers, but I will probably throw this up on my blog when I get it back up.
<?php
function googleResult($listItem) {
// given a LIST ITEM element, this will validate, and return an array for that LI entry as an inline result from google.
/*
* <li class='g w0'>
* <h3 class='r'>
* <a href='the URL' class='l'>
* Description <em>description</em>
* </a>
* </h3>
* </li>
*
UPDATE:
This function will now look for any subcontainer that has an href, it doesn't have to be an H3
this will make it work with a few more formatted search results.
*/
$listItem = $listItem->childNodes;
// Yes I don't use instanceof - I guess you'll have to deal.
foreach($listItem as $element) {
if(is_object($element) && get_class($element) == 'DOMElement' && $element->hasChildNodes()) {
$hrefContainer = $element->childNodes;
foreach($hrefContainer as $element2) {
if(is_object($element2) && get_class($element2) == 'DOMElement' && $element2->nodeName == 'a' && $element2->hasAttribute('href')) {
$anchor = $element2;
unset($h3);
unset($element2);
break;
} else {
//print __LINE__ ." :: Breaking out of loop (normal result) element is not an annchor Element='".$element2->nodeName."'\n";
}
}
unset($element);
unset($listItem);
break;
}
}
if(empty($anchor) || !is_object($anchor) || get_class($anchor) != 'DOMElement') {
//print __LINE__ ." :: Returning false, did not locate anchor through iteration...";
return false;
}
$href = $anchor->getAttribute('href');
if(empty($href)) {
//print __LINE__ ." :: Found anchor object, could not read href attribute / href is empty? href='$href'\n";
return false;
}
$description = $anchor->childNodes;
$urlDescription = '';
foreach($description as $words) {
$name = trim($words->nodeName);
if($name == 'em' || $name == '#text' || $name == 'b') {
if(!empty($words->nodeValue)) {
$text = trim($words->nodeValue);
$urlDescription = $urlDescription . $text . ' ';
}
}
}
$urlDescription = htmlspecialchars_decode($urlDescription, ENT_QUOTES);
$urlDescription = trim($urlDescription);
return array('description' => $urlDescription, 'href' => $href);
}
philipwaynerollins at gmail dot com ¶
15 years ago
You can get the "innerHTML" by nodeValue so
<?php
$doc = new DOMDocument( );
$ele = $doc->createElement( 'p', 'Sensei Ninja' );
print $ele->nodeValue;
?>
You can even set it if you want
<?php
$doc = new DOMDocument( );
$ele = $doc->createElement( 'p' );
$ele->nodeValue = 'Sensei Ninja';
$doc->appendChild( $ele );
print $doc->saveHTML( );
?>
sean at lookin3d dot com ¶
18 years ago
$xmlDoc=<<<XML
<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>
XML;
$xml= new DOMDocument();
$xml->preserveWhiteSpace=false;
$xml->loadXML($xmlDoc);
print_r(xml2array($xml));
function xml2array($n)
{
$return=array();
foreach($n->childNodes as $nc)
($nc->hasChildNodes())
?($n->firstChild->nodeName== $n->lastChild->nodeName&&$n->childNodes->length>1)
?$return[$nc->nodeName][]=xml2array($item)
:$return[$nc->nodeName]=xml2array($nc)
:$return=$nc->nodeValue;
return $return;
}
nospam at ya dot ru ¶
15 years ago
<?PHP
function dom2array_full($node){
$result = array();
if($node->nodeType == XML_TEXT_NODE) {
$result = $node->nodeValue;
}
else {
if($node->hasAttributes()) {
$attributes = $node->attributes;
if(!is_null($attributes))
foreach ($attributes as $index=>$attr)
$result[$attr->name] = $attr->value;
}
if($node->hasChildNodes()){
$children = $node->childNodes;
for($i=0;$i<$children->length;$i++) {
$child = $children->item($i);
if($child->nodeName != '#text')
if(!isset($result[$child->nodeName]))
$result[$child->nodeName] = dom2array($child);
else {
$aux = $result[$child->nodeName];
$result[$child->nodeName] = array( $aux );
$result[$child->nodeName][] = dom2array($child);
}
}
}
}
return $result;
}
?>
danf dot 1979 at []gmail[] dot com ¶
16 years ago
This is a couple of classes to deal with yahoo yui menu.
/*
$menubar = new MenuBar();
$file = new Menu("File");
$file->setAttribute("href", "http://file.com");
$quit = new Menu("Quit");
$quit->setAttribute("href", "http://quit.com");
$file->appendChild($quit);
$menubar->appendChild($file);
echo $menubar->grab();
*/
//
// Author: Daniel Queirolo.
// LGPL
//
/** ---------------------------------
/** Class MenuBar()
/** Creates a the menubar and appends
/** yuimenubaritems to it.
/** ---------------------------------*/
class MenuBar extends DOMDocument
{
public $menuID = "nav_menu"; // holds the css id that javascript yui menu code should have to recognize
private $UL; // This node holds every menu, This is THE node.
/** ---------------------------------
/** Constructor
/** Generates a menubar skeleton and the UL node
/** ---------------------------------*/
public function __construct() {
parent::__construct();
$rootdiv = parent::createElement("div");
$rootdiv->setAttribute("class", "yui-skin-sam");
parent::appendChild($rootdiv);
$yui_menubar = parent::createElement("div");
$yui_menubar->setAttribute("id", $this->menuID);
$yui_menubar->setAttribute("class", "yuimenubar");
$rootdiv->appendChild($yui_menubar);
$bd = parent::createElement("div");
$bd->setAttribute("class", "bd");
$yui_menubar->appendChild($bd);
$ul = parent::createElement("ul");
$ul->setAttribute("class", "first-of-type");
// ALL Menu() instances ocurr inside an <ul> tag.
$this->UL = $bd->appendChild($ul);
}
/** ---------------------------------
/** appendChild()
/** Appends a new yuimenubaritem to the menubar UL node.
/** This function changes <li> and <a> classes to yuiMENUBARsomething
/** ---------------------------------*/
public function appendChild($child) {
$li = parent::importNode($child->LI, true);
$li->setAttribute("class", "yuimenubaritem");
$li->getElementsByTagName("a")->item(0)->setAttribute("class", "yuimenubaritemlabel");
$this->UL->appendChild($li);
}
public function grab() {
return parent::saveHTML();
}
}
/** ---------------------------------
/** Class Menu()
/** Creates a yuimenuitem li node
/** ---------------------------------*/
class Menu extends DOMDocument {
public $LI; // stores the <li> node (THE link) that will be exported to MenuBar() or used on appendChild()
/** ---------------------------------
/** Constructor
/** Generates a yuimenuitem li node
/** No yuimenubar items are created here. MenuBar handles that.
/** ---------------------------------*/
public function __construct($link_name) {
parent::__construct();
$li = parent::createElement("li");
$li->setAttribute("class", "yuimenuitem");
// LI node stores THE link.
// if appendChild is used, the new (sub) Menu() would be LI node child.
$this->LI = parent::appendChild($li);
$a = parent::createElement("a", $link_name);
$a->setAttribute("class", "yuimenuitemlabel");
$li->appendChild($a);
$this->li = $li;
$this->a = $a;
}
/** ---------------------------------
/** appendChild
/** Appends a (sub) Menu() to current Menu() in LI
/** ---------------------------------*/
public function appendChild($child) {
$yuimenu = parent::createElement("div");
$yuimenu->setAttribute("class", "yuimenu");
$this->LI->appendChild($yuimenu);
$bd = parent::createElement("div");
$bd->setAttribute("class", "bd");
$yuimenu->appendChild($bd);
$ul = parent::createElement("ul");
$bd->appendChild($ul);
// child->NODE holds THE link from the new child (from child's __construct())
$ul->appendChild(parent::importNode($child->LI, true));
}
public function setAttribute($name, $value, $node="a") {
if ($node == "a") {
$this->a->setAttribute($name, $value);
}
else {
$this->li->setAttribute($name, $value);
}
}
}