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

  • Home
  • SEARCH
  • 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 795509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:29:31+00:00 2026-05-14T22:29:31+00:00

Hey guys, I have the following HTML structure that I am trying to pull

  • 0

Hey guys, I have the following HTML structure that I am trying to pull information from:

// Product 1
<div class="productName">
 <span id="product-name-1">Product Name 1</span>
</div>

<div class="productDetail">            
 <span class="warehouse">Warehouse 1, ACT</span>                
 <span class="quantityInStock">25</span>
</div>

// Product 2
<div class="productName">
 <span id="product-name-2">Product Name 2</span>
</div>

<div class="productDetail">            
 <span class="warehouse">Warehouse 2, ACT</span>                
 <span class="quantityInStock">25</span>
</div>

…

// Product X
<div class="productName">
 <span id="product-name-X">Product Name X</span>
</div>

<div class="productDetail">            
 <span class="warehouse">Warehouse X, ACT</span>                
 <span class="quantityInStock">25</span>
</div>

I don’t have control of the source html and as you’ll see productName and it’s accompanying productDetail are not contained within a common element.

Now, I am using the following php code to try and parse the page.

$html = new DOMDocument();
$html->loadHtmlFile('product_test.html');

$xPath = new DOMXPath($html);

$domQuery = '//div[@class="productName"]|//div[@class="productDetail"]';

$entries = $xPath->query($domQuery);

foreach ($entries as $entry) { 
 echo "Detail: " . $entry->nodeValue) . "<br />\n";
}

Which prints the following:

Detail: Product Name 1
Detail: Warehouse 1, ACT
Detail: 25
Detail: Product Name 2
Detail: Warehouse 2, ACT
Detail: 25
Detail: Product Name X
Detail: Warehouse X, ACT
Detail: 25

Now, this is close to what I want. But I need to do some processing on each Product, Warehouse and Quantity stock and can’t figure out how to parse it out into separate product groups. The final output I am after is something like:

Product 1:
Name: Product Name 1
Warehouse: Warehouse 1, ACT
Stock: 25

Product 2:
Name: Product Name 2
Warehouse: Warehouse 2, ACT
Stock: 25 

I can’t just figure it out, and I can’t wrap my head around this DOM stuff as the elements don’t quite work the same as a standard array.

If anyone can assist, or point me in the right direction I will be ever appreciative.

  • 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-14T22:29:32+00:00Added an answer on May 14, 2026 at 10:29 pm

    Maybe not the most efficient way but

    $html = new DOMDocument();
    $html->loadHtmlFile('test2.php');
    
    $xPath = new DOMXPath($html);
    
    foreach( $xPath->query('//div[@class="productName"]') as $prodName ) { 
      $prodDetail = $xPath->query('following-sibling::div[@class="productDetail"][1]', $prodName);
      // <-- todo: test if there is one item here -->
      $prodDetail = $prodDetail->item(0);
      echo "Name: " . $prodName->nodeValue . "<br />\n";
      echo "Detail: " . $prodDetail->nodeValue . "<br />\n";
      echo "----\n";
    }
    

    prints

    Name: 
     Product Name 1
    <br />
    Detail:             
     Warehouse 1, ACT                
     25
    <br />
    ----
    Name: 
     Product Name 2
    <br />
    Detail:             
     Warehouse 2, ACT                
     25
    <br />
    ----
    Name: 
     Product Name X
    <br />
    Detail:             
     Warehouse X, ACT                
     25
    <br />
    ----
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.