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  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.