user3619039
user3619039

Reputation: 47

Search particular node value from XML

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

Answers (1)

Kevin
Kevin

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>";
}

Sample Output

Upvotes: 1

Related Questions