dpthuyh1678 2010-05-16 08:20
浏览 14
已采纳

需要帮助访问PHP DOM元素

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

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条回答 默认 最新

  • dsbgltg159136540 2010-05-16 08:49
    关注

    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 />
    ";
      echo "Detail: " . $prodDetail->nodeValue . "<br />
    ";
      echo "----
    ";
    }
    

    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 />
    ----
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?