Jump to content

PHP DOMDocument, finding specific tags in a very easy example [here my approach]


dilbertone

Recommended Posts

good evening dear Community,  8)

 

Well first of all: felize Navidad - I wanna wish you a Merry Christmas!!

 

Today i'm trying to debug a little DOMDocument object in PHP. Ideally it'd be nice if I could get DOMDocument to output in a array-like format, to store the data in a database!

 

My example: head over to the url -

see the example: the target http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=8880   

 

I investigated the Sourcecode:

 

I want to filter out the data that that is in the following class <div class="floatbox">

 

See the sourcecode:

 

<span class="grey"> <span style="font-size:x-small;">></span></span>
<a class="navLink" href="http://dms-schule.bildung.hessen.de/suchen/index.html" title="Suchformulare zum hessischen schulischen Bildungssystem">suche</a>
              </div>
            </div>
          <!-- begin of text -->
           <h3>Siegfried-Pickert Schule</h3>
<div class="floatbox">

 

 

See my approach: Here is the solution return the labels and values in a formatted array ready for input to mysql!

<?php

$dom = new DOMDocument();
@$dom->loadHTMLFile('http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=8880');
$divElement = $dom->getElementById('floatbox');

$innerHTML= '';
$children = $divElement->childNodes;
foreach ($children as $child) {
$innerHTML = $child->ownerDocument->saveXML( $child );

$doc = new DOMDocument();
$doc->loadHTML($innerHTML);
//$divElementNew = $dom->getElementsByTagName('td');
$divElementNew = $dom->getElementsByTagname('td');

    /*** the array to return ***/
    $out = array();
    foreach ($divElementNew as $item)
    {
        /*** add node value to the out array ***/
        $out[] = $item->nodeValue;
    }

echo '<pre>';
print_r($out);
echo '</pre>';

} 

 

 

well Duhh: this outputs lot of garbage. The code spits out a lot of html anyway.

What can i do to get a more cleaned up code!?

 

What is wrong with the idea of using this attribute:

 

 $dom->getElementById('floatbox');

 

any idea!?

 

any and all help will greatly appreciated.

 

season-greetings

db1  ;)

Hello dear friends,

 

found out the following:

 

$dom->getElementById('floatbox');

 

...in original html it's not an id, it's a class.

 

So i have to rewrite like so:

 

$divElement = $dom->getElementByClass('floatbox');

 

Well i try out this solution.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.