I am using DOMDocument to parse an html document and get some data out of it. Following is the structure of sub-tree of DOM
<div id="tab1">
<div class="some class name"></div>
<div class="some other class name">arbitrary data and nodes</div>
<p> lot of paragraphs to follow </p>
<p> paragraphs </p>
<p> paragraphs </p>
<p> paragraphs </p>
<p> paragraphs </p>
<br />
<br />
<br />
<br />
<br />
<table />
<table />
<table />
<table />
</div>
I do not want first two children of tab1. I am using following PHP Code
<?php
$urlArray = file('sitemap.txt');
$dataSet = array();
foreach($urlArray as $url){
$scrapedData = file_get_contents('./scraped-site/'.trim($url));
$doc = new DOMDocument();
@$doc->loadHTML($scrapedData);
$domXpathDoc = new DOMXPath($doc);
$results = '';
$xpathArray = array(
'info'=>'//*[@id="tabs1"]',
);
$set = array();
foreach($xpathArray as $field => $xpath){
$domNodeList = $domXpathDoc->query($xpath);
foreach($domNodeList as $node){
foreach ($node->childNodes as $child) {
$set[] = $child->ownerDocument->saveXML( $child );
}
}
}
$dataSet[] = $set;
}
The code given gives me all children how can I selectively avoid any node?
[EDIT2: I tried the answer below (I learned 🙂 ). This is working for me:
Basically it tells the xpath to ignore all elements with name ‘div’. You can ignore more than one element like this:
Only showing elements after the first two would work like this: