PHP: DOMNode::appendChild to an array of Elements - php

I am using DOMDocument to parse an XML file. I loop through the different Elements and see if any of them is missing and I fill an array with a createElement, with the error message. At the end I'm trying to appendChild that array but I always get the same error message:
Uncaught exception 'DOMException' with message 'Wrong Document Error'
DOMNode->appendChild(Object(DOMElement))
1 {main}
thrown in /xxx/xxx.php on line 235
PHP Fatal error: Call to undefined method DOMElement::item() in /xxx/xxx.php on line 235.
the code is as follow:
$SMQuery = new DOMDocument();
$SMQuery->loadXML($params);
$response = $SMQuery->createElement('SMreply');
$errors = array();
if (!$reqtyp = $SMQuery->getElementsByTagName("tag1"))
{$errors[] = $SMQuery->createElement('error', 'tag1 Element is missing');}
if (!$reqtyp = $SMQuery->getElementsByTagName("tag2"))
{$errors[] = $SMQuery->createElement('error', 'tag2 Element is missing');}
......
if(!empty($errors))
{
foreach($errors as $error) {
$response->appendChild($error); <==== this line is causing the error !!!
}
}
Any help is much appreciated.
Cheers,
Riki.

You don't show where $response is being defined, but if it's the result of another new DOMDocument(), then that explains you error - you can't add nodes from one DOM object to another directly. It has to be imported first via ->importNode(). Only after that can you actually append it.

Related

Is it possible to use DOM on an external webpage?

I like to use the list on this page: list of teams of soccerclub
But I always get this error
Fatal error: Uncaught Error: Call to a member function find() on null in /home/u562375926/domains/lucswebsite.tech/public_html/resp/scrapvv.php:152 Stack trace: #0 {main} thrown in /home/u562375926/domains/lucswebsite.tech/public_html/resp/scrapvv.php on line 152
using this code:
<?php
include('simple_html_dom.php');
$file = 'https://www.voetbalvlaanderen.be/club/1771/ploegen';
$html = new simple_html_dom();
$html->load_file($file);
$club = $html->find('div',8)->find(h2);
echo $club. '<br>';
?>
I have used the same page in ParseHub and I get all a hrefs and the text of the corresponding spans.
Is it possible that DOM is not working on that page?

PHP simple HTML DOM parser errors

I've started writing a scraper for one site that will also have a crawler, since I need to go through some links, but I'm getting this error :
PHP Fatal error: Uncaught Error: Call to a member function find() on
null in D:\Projekti\hemrank\simple_html_dom.php:1129 Stack trace:
0 D:\Projekti\hemrank\scrapeit.php(37): simple_html_dom->find('ul')
1 D:\Projekti\hemrank\scrapeit.php(19): ScrapeIt->getAllAddresses()
2 D:\Projekti\hemrank\scrapeit.php(55): ScrapeIt->run()
3 {main} thrown in D:\Projekti\hemrank\simple_html_dom.php on line 1129
When I var_dump the $html variable I get the full html with all the tags, etc, that's why it's strange to me that it says "Call to a member function find() on null", when there's actually value in the $html. Here's the part of the code that's not working :
$html = new simple_html_dom();
$html->load_file($baseurl);
if(empty($html)){echo "HTTP Response not received!<br/>\n";exit;}
$links = array();
foreach ($html->find('ul') as $ul) {
if(!empty($ul) && (count($ul)>0))
foreach ($ul->find('li') as $li) {
if(!empty($li) && (count($li)>0))
foreach ($li->find('a') as $a) {
$links[] = $a->href;
}
else
die("NOT AVAILABLE");
}
}
return $links;
}
Is this a common problem with PHP simple HTML DOM parser, is there a solution or should I switch to some other kind of scraping?
I just searched for the lib you are using, this is line 1129:
return $this->root->find($selector, $idx, $lowercase);
So your error message is telling you that $this->root inside the class is null, therefore no find() method exists!
I'm no expert on the lib, as I use the awesome DOMDocument for parsing HTML, but hopefully this should help you understand what has happened.
Also, $html will never be empty in that code of yours, you already populated it when you instantiated it!
I suggest the following change:
$html->load_file($baseurl); to $html = file_get_html($baseurl);
On my VPS server it works with $html->load_file($baseurl); but on my dedicated local server it only works with $html = file_get_html($baseurl);
This solved my problem:
- Call to a member function find() on null
- simple_html_dom.php on line 1129

$and condition in php causing Uncaught exception 'MongoException' with message 'Invalid object ID'

I have a selection criteria in $Select which selects a document.The document has username and a couple of other document's object id.After selecting the document it searches for "Another_id" field and stores it in $Another_id variable
$Select = array("Username"=>$user, "SomeOther_id"=>$SomeOtherDocID);
$doc = $db->$collection->find($Select);
foreach($doc as $o=>$a) {
foreach($a as $k=>$v) {
if($k=="Another_id")
echo $Another_id=$v;
}
}
$a_id=array("_id"=>new MongoId($Another_id));
Now $Another_id being an object_id will have it's own document in it's own collection. So to access that another collection document i've got to convert $Another_id to Object id in the last line.
Here I'm getting error: Uncaught exception 'MongoException' with message 'Invalid object ID'
I believe it's because the selection criteria $Select is not working as
echo $Another_id; does not output anything.
How to solve this error?

Parsing HTML Table - PHP

I am trying to parse many HTML tables, with the URLs stored in the database. The current problem with my code is that it will fail on a different table every time. Here is the part of the code that gets the error:
while ($sqlrow = mysqli_fetch_row($res)) {
echo "Started Processing Table " . $tables . PHP_EOL;
$tables++;
$data = file_get_contents($sqlrow[1]);
$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;
$teamtable = $dom->getElementById("reTeamTable");
$teamrows = $teamtable->getElementsByTagName('tr');
The lines that usually fails is either the "getElementById" command or the "getElementsByTagName" command. The error I am getting is: "PHP Fatal error: Call to a member function getElementsByTagName() on a non-object in /scouting/teamlist.php on line 20". I don't understand why this is getting an error on a different URL every time.
Its means that $dom doesnt find element with id="reTeamTable" ( $teamtable is null ). Before call getElementsByTagName , check $teamtable on empty.

Get the nodeValue for a tag

I am trying to get the nodeValue of any tag found in an html page but am getting an error and i can't figure what is causing that error. Fatal error: Fatal error: Call to undefined method DOMDocument::getElementByTagName() in C:\xampplite\htdocs\msite\getscriptnodeValue.php on line 5..here is my code..Can anyone please help me??Thnxx in advance.
$file=file_get_contents('test.txt');
$doc=new DOMDocument();
#$doc->loadHTML('<?xml encoding="UTF-8">'.$file);
$data=$doc->getElementByTagName('div');
for($i=0;$i<$data->length;$i++){
$getTag=$data->item($i);
echo $getTag->nodeValue;
echo"<br/>";
}
The method name is getElementsByTagName() (with an S) not getElementByTagName().
Change to:
$data=$doc->getElementsByTagName('div');
// ^ missing s

Categories