Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:18:07+00:00 2026-05-27T07:18:07+00:00

I am parsing an HTML page with DOM and XPath in PHP. I have

  • 0

I am parsing an HTML page with DOM and XPath in PHP.

I have to fetch a nested <Table...></table> from the HTML.

I have defined a query using FirePath in the browser which is pointing to

html/body/table[2]/tbody/tr/td[2]/table[2]/tbody/tr/td/table

When I run the code it says DOMNodeList is fetched having length 0. My objective is to spout out the queried <Table> as a string. This is an HTML scraping script in PHP.

Below is the function. Please help me how can I extract the required <table>

$pageUrl = "http://www.boc.cn/sourcedb/whpj/enindex.html";

getExchangeRateTable($pageUrl);


function getExchangeRateTable($url){
    $htmlTable = "";
    $xPathTable = nulll;
    $xPathQuery1 = "html/body/table[2]/tbody/tr/td[2]/table[2]/tbody/tr/td/table";

    if(strlen($url)==0){die('Argument exception: method call [getExchangeRateTable] expects a string of URL!');}

    // initialize objects
    $page = tidyit($url);
    $dom = new DOMDocument();
    $dom->loadHTML($page);
    $xpath = new DOMXPath($dom);

    // $elements is sppearing as DOMNodeList
    $elements = $xpath->query($xPathQuery1);

    // print_r($elements);
    foreach($elements as $e){
        $e->firstChild->nodeValue;  
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:18:08+00:00Added an answer on May 27, 2026 at 7:18 am

    Remove the tbody’s from your XPath query – they are in most cases inserted by your browser, as is with the page you are trying to scrape.

    /html/body/table[2]/tr/td[2]/table[2]/tr/td/table
    

    This will most likely work.

    However, its probaly more safe to use a different XPath. Following XPath will select the first th based on it’s textual content, then select the tr’s parent – a tbody or table:

    //th[contains(text(),'Currency Name')]/parent::tr/parent::*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am parsing an html page using XmlSlurper and HtmlCleaner, i have the GPathResult
Got this error when parsing my html page using XPATH.. i am also using
I have a large nested array that I'm generating from parsing a CSV file
I am parsing html page now any of string comes from html page I
I am using HtmlAgility pack for parsing the html page. I am able to
I have an app that is parsing the html page and extracts some text
I'm parsing HTML page with XPath and want to grab whole text of some
I am parsing a table of a HTML page but when I display the
I am parsing an html page using the Html Agility Pack . I am
Today when I was parsing one page with Simple HTML DOM parser I didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.