+1 vote
247 views
in Programming by (440 points)

I am new in DOM and want to collect a particular node value of a div my environment is in PHP.

I am trying to get node values from DOM but not getting success, I hope any one of you help me out Thanks!

closed

1 Answer

+1 vote
by Expert (5.9k points)
edited by
 
Best answer

DOM is a interface for the programming languages like HTML, XHTML and XML, Here we are talikg about HTML so in HTML DOM there is a node which contains some values which is called node values lets see a example :

Say we have a url : $domain = www.xyz.com

 

$doc = new DOMDocument();

// Get the sourcecode of the domain

@$doc->loadHTMLFile($domain);

 

//here we are collecting all div tags from web page

$div = $doc->getElementsByTagName('div');

 

foreach($books as $div)

{

 

$class = $div->getAttribute('class');

if (strpos($class, 'price') !== false)

{

 

echo $class->nodeValue;

 

}

}




In this example we are using PHP and printing all node values of class price.

by (440 points)
thanks for such a nice explanation.
by (300 points)

hi, duke 
can you tell me were did you get $books variables, because you did not define or assign a value on it ..and you execute foreach loop of $book variables.

I think code should be 

$doc = new DOMDocument();

// Get the sourcecode of the domain

@$doc->loadHTMLFile($domain);

 

//here we are collecting all div tags from web page

$books = $doc->getElementsByTagName('div');

 

foreach($books as $div)

{

    if($div->getAttribute('class') == "price"){

          echo $class->nodeValue;

     }

     // and we can do this below code also 

    if (strpos($class, 'price') !== false)

      {

           echo $class->nodeValue;

      }

 

}

happy coding!

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated