Page 1 of 1

XMLReader and attributes

Posted: Sat Aug 08, 2009 10:55 am
by ruttegar
Hey.

I have 6mb XML file to sort through, assign variables to certain data and insert into MySQL db. all the information is going in great apart from the following...

Code: Select all

 
<descriptions>
    <extra_description lang="es"><![CDATA[lots of spanish text]]></extra_description>
    <long_description lang="es"><![CDATA[more spanish text and things]]></long_description>
   
    <extra_description lang="en"><![CDATA[English text and extra info]]></extra_description>
    <long_description lang="en"><![CDATA[Lots more english text and writing]]></long_description>
    
</descriptions>
</property>
 
i have so far been using XMLreader and code like this for example:

Code: Select all

 
if($reader->nodeType == 1) // XMLReader::ELEMENT
    {
        $name = $reader->name;
    }
if($reader->nodeType ==  4) // XMLReader::CDATA
    {
        $value = $reader->value;
    }
if($name == 'extra_description')    {   $extra = $value;    }
if($name == 'long_description')     {   $description = $value;  }
 
unfortunately there are two identical tags for the extra_description and only the "lang" attribute to differentiate them.

so my Q is...how do i isolate a tag by its name AND one of its attributes???? i only want the english.

in another part of the script i have used...

Code: Select all

 
$reader->getAttribute("url");
 
...to retrieve an attribute value but havent been able to figure out a way to make the same method work for this.

Many Thanks

Rutt

Re: XMLReader and attributes

Posted: Mon Aug 10, 2009 7:34 am
by Eran
Put the two conditions in the same statement

Code: Select all

if($reader -> name == 'long_description' && $reader -> getAttrbiute('lang') == 'en') {
...
} 
etc