XMLReader and attributes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ruttegar
Forum Newbie
Posts: 1
Joined: Sat Aug 08, 2009 10:32 am

XMLReader and attributes

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: XMLReader and attributes

Post by Eran »

Put the two conditions in the same statement

Code: Select all

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