Reputation: 47
This is my PHP/XML code. I just want to search the particular $searchterm
from this:
$doc = new DOMDocument;
$doc->load('authentication.xml');
$xpath = new DOMXPath($doc);
$searchTerm = "N0A097016646";
$lineNo = 0;
foreach ($xpath->query('//device/following::comment()') as $comment){
$serial= $xpath->query('//device', $comment)->item($lineNo)->textContent;
echo "Line number: ".$comment->getLineNo()." Device number: ".$serial." Comented number: ".$comment->textContent."<br>";
$lineNo++;
}
Where should I put the $searchTerm
variable to search that particular node?
Upvotes: 1
Views: 105
Reputation: 41903
As your question stands you haven't use the $searchTerm
.
Use it with =
(equal)
text() = '$searchTerm'
Code:
$searchTerm = "N0A097016646";
foreach ($xpath->query("//device[text() = '$searchTerm']/following::comment()") as $comment){
echo "Line number: ".$comment->getLineNo(). '<br/>'.
" Device number: ".$searchTerm.'<br/>'.
" Comented number: ". $comment->textContent."<hr>";
}
Upvotes: 1