How to Debug seemingly empty DOM objects?
For a DomDocument
I var_dump
using the xml output.
var_dump($dom->saveXML());
For a DOMElement
, I use (as seen here):
var_dump($domElement->ownerDocument->saveXML($domElement));
But DOMNodeList
, I have no idea. Maybe you have to attach/append it to a DomDocument, and then var_dump it.
And btw, not showing internals of a DomDocument is reported (here: Reflection).
Debugging of object of DOMXPath and DOMDocument
This is a bug:
- Bug #48527: DOM XML classes do not expose properties to Reflection
For some reason, DOM*
classes do not expose their properties, neither to Reflection, nor to any other function capable of inspecting objects. You'd have to write a custom inspector that collects the properties manually.
Trying to get values from a html page with PHP DOMDocument
This is what I have got to work from your code so far...
$doc = new DOMDocument();
$doc->validateOnParse = true;
$doc->preserveWhiteSpace = false;
$doc->loadHTML($page);
$ReadIDMapsRow = ['column1_name' => 'CAR', 'column1_id' => 'car7'];
$Column1Name = $ReadIDMapsRow['column1_name'];
$Column1Value = $doc->getElementById($ReadIDMapsRow['column1_id']);
$Column1ValueText = $Column1Value->textContent;
echo $Column1Name.PHP_EOL;
echo $Column1ValueText.PHP_EOL;
which gives...
CAR
90
how to get a raw from a DOMNodeList
DOMNodeList::item()
will give a DOMNode
in the DOMNodeList
by index. For example, to get the first node in the list:
$dom->getElementsByTagName('url')->item(0)
To get the actual data that the element contains, use code something like this:
if ($node = $dom->getElementsByTagName('url')->item(0)) {
$url = $node->nodeValue;
} else {
// Element does not exist, handle error here
}
Get entire BODY content using PHP DOM DOCUMENT
You can pass the body DOMElement to either DOMDocument::saveHTML() or DOMDocument::saveHTMLFile(), e.g.
<?php
$doc = new DOMDocument;
$doc->loadhtmlfile('http://stackoverflow.com');
$body = $doc->getElementsByTagName('body');
if ( $body && 0<$body->length ) {
$body = $body->item(0);
echo $doc->savehtml($body);
}
prints
Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : p in http://stackoverflow.com, line: 2843 [...]
<body class="home-page">
<noscript><div id="noscript-padding"></div></noscript>
<div id="notify-container"></div>
<div id="overlay-header"></div>
<div id="custom-header"></div>
<div class="container">
<div id="header">
<div id="portalLink">
[...]
Can't Load And Parse DOM Object
This is an example from the manual: DOMDocument
<?php
$content = file_get_contents("http://www.example.com/example.php");
$xml = new DOMDocument();
$xml->loadHTML($content);
// Empty array to hold all links to return
$links = array();
//Loop through each <a> tag in the dom and add it to the link array
foreach($xml->getElementsByTagName('a') as $link) {
$links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
}
print_r($links);
?>
Related Topics
How to Convert a String to JSON Object in PHP
A Better Approach Than Storing MySQL Password in Plain Text in Config File
How to Use Class Methods as Callbacks
Is It Recommended to Store PHP Sessions in Memcache
Download CSV File Using "Ajax"
PHP - Import CSV File to MySQL Database Using Load Data Infile
How to Check That an Object Is Empty in PHP
Get the First Letter of Each Word in a String
Php: Settings Memory_Limits > 1024M Does Not Work
Convert Jpg/Gif Image to Png in PHP
Wamp/MySQL Errors Not in Correct Language
How to Convert Seconds to Time Format
How to Pass Get Parameters to Laravel from with Get Method
When *Not* to Use Prepared Statements
How to Display Unicode Data with PHP