Skip to content
Advertisement

how to get multiple data from xpath query?

This is HTML page (test.html)

<div id = 'mainid'>
    <div id = 'subid'>
        Name: ABC
    </div>
    <div id = 'subid'>
        Country: USA
    </div>
    <div id = 'subid'>
        Date of birth: 15 Feb 1985
    </div>
</div>
<div id = 'mainid'>
    <div id = 'subid'>
        Name: Jisan
    </div>
    <div id = 'subid'>
        Country: Japan
    </div>
    <div id = 'subid'>
        Date of birth: 15 Feb 1985
    </div>
</div>
<div id = 'mainid'>
    <div id = 'subid'>
        Name: Mr Barman
    </div>
    <div id = 'subid'>
        Country: Canada
    </div>
    <div id = 'subid'>
        Date of birth: 15 Feb 1985
    </div>
</div>

The PHP Code here

$file = $DOCUMENT_ROOT. "test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

$Querys = $xpath->query("*//div[@id='mainid']");
foreach ($Querys as $Querys) {
    echo $Name  = Please help me about this code;
    echo $Country   = Please help me about this code;
    echo $DOB   = Please help me about this code;
}

NOTE: i want to get result like this

Name: ABC, Country: USA, Date of birth: 15 Feb 1985.
Name: Jisan, Country: Japan, Date of birth: 15 Feb 1985.
Name: Mr Barman, Country: Canada, Date of birth: 15 Feb 1985.

Advertisement

Answer

One approach is to use the contextnode parameter to DOMXPath::query to do a subquery on each of the mainid elements for the child subids. Something like this:

$mainElements = $xpath->query("*//div[@id='mainid']");
foreach ($mainElements as $mainElement) {
    $subElements = $xpath->query("div[@id='subid']", $mainElement);

    if ($subElements && $subElements->length == 3) {
        $Name = trim($subElements[0]->nodeValue);
        $Country = trim($subElements[1]->nodeValue);
        $DOB = trim($subElements[2]->nodeValue);
        echo "$Name, $Country, $DOBn";
    } else {
        echo "Invalid number of sub-elements.n";
    }   
}

Note that the trim calls are necessary or you will end up with all of the whitespace from the original document in your output.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement