Jump to content

How to convert DOMDocument to XMLReader


StefanRSA

Recommended Posts

I need to import a large XML file into my DB (2Gb)
I tried to use DOMDocument and it failed cause of size and read up and understand that I should rather use XMLReader.

Google search do not help much to give me an indication what I should different to read the document with XMLReader...

Can anybody please help me to either point me in the right direction to fix this document I created or even help me to rewrite the parser... I just cannot get my head arounf this problem... :confused:  :
My DOMDocument is as follow:

 

#!/usr/bin/php –q
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');
$l = mysql_connect  ( XXXXX);
$total='0';
    $xml = "http://xxxxxxxxxxxxxxxxxxxx";
    $doc = new DOMDocument();
    $doc->load( $xml);
    ## lets read the code block
    $records = $doc->getElementsByTagName( "Property" );
    foreach( $records as $result )    {

    $adnr = $result->getElementsByTagName( "ID" );          // adnr
    $adnr = $adnr->item(0)->nodeValue;
    $town = $town->getElementsByTagName( "town" );          // town
    $town = $town->item(0)->nodeValue;
//////////////////////////////////////////////////////////////////////////////// Add Pics
    $pic1= $result->getElementsByTagName( "image" );
    $pic1 = $pic1->item('1')->nodeValue;
    $pic2= $result->getElementsByTagName( "image" );
    $pic2 = $pic2->item('2')->nodeValue;
    $pic3= $result->getElementsByTagName( "image" );
    $pic3 = $pic3->item('3')->nodeValue;

}
?>
Link to comment
https://forums.phpfreaks.com/topic/281864-how-to-convert-domdocument-to-xmlreader/
Share on other sites

I am trying the XMLReader as follow: (This is not working... Any help?)

#!/usr/bin/php –q
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');
$url='http://xxxxxxx.xxx';
    $reader = new XMLReader();
    $reader->open($url);

    while($reader->read()){
    if($reader->nodeType == XMLReader::ELEMENT) $nodeName = $reader->name;
    if($reader->nodeType == XMLReader::TEXT || $reader->nodeType == XMLReader::CDATA)    {
    if ($nodeName == 'ID'){
    $adnr = $reader->value;
    echo $adnr.'<br>';
    }
    if ($nodeName == 'Country'){
    $country= $reader->value;
    }
        if ($nodeName == 'Location1'){
    $province = $reader->value;
    }
        if ($nodeName == 'Location2'){
    $region = $reader->value;
    }
        if ($nodeName == 'Location3'){
    $town = $reader->value;
    }
        if ($nodeName == 'Price'){
    $price =$reader->getAttribute('4');
    echo 'Price='.$price.'/n/n';
    }
        if ($nodeName == 'Beds'){
    $bedrooms = $reader->value;
    }
        if ($nodeName == 'Ref'){
    $ref = $reader->value;
    }
        if ($nodeName == 'Name'){
    $bname = $reader->value;
    }
        if ($nodeName == 'Sector'){
    $sector = $reader->value;
    $sector=trim(preg_replace('/\s+/', ' ', $sector));
    }
        if ($nodeName == 'Type'){
    $type = $reader->value;
    $hometype = trim($type.' '.$sector);
    $hometype=trim(preg_replace('/\s+/', ' ', $hometype));
    }
        if ($nodeName == 'Key_selling_points'){
    $keyfield = $reader->value;
    }
        if ($nodeName == 'Baths'){
    $bathrooms = $reader->value;
    }
        if ($nodeName == 'Full_Desc'){
    $fullt = $reader->value;
    }    
        if ($nodeName == 'image'){
    $pic1 = $reader->getAttribute('1')->value;
    }
        if ($nodeName == 'image'){
    $pic2 = $reader->getAttribute('2')->value;
    }
        if ($nodeName == 'image'){
    $pic3 = $reader->getAttribute('3')->value;
    }
        if ($nodeName == 'image'){
    $pic4 = $reader->getAttribute('4')->value;
    }
        if ($nodeName == 'image'){
    $pic5 = $reader->getAttribute('5')->value;
    }
        if ($nodeName == 'image'){
    $pic6 = $reader->getAttribute('6')->value;
    }


/////////////////////////////////////////
    }
    $count='0';      
    if($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'Property')    {
    $count++;
    }
    }
    $reader->close();
echo $count;
?>

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.