Nicola Lorenzini
Nicola Lorenzini

Reputation: 11

XML reader and skip certain nodes

After found how to read every XML node and value with XMLreader, i have another problem.

im doing this:

    $tablename = 'my_table_name';
    $reader = new XMLReader;
    $reader->xml($MyXml);
    while($reader->read()) {

        if($reader->nodeType === XMLReader::ELEMENT) {


            if(CheckIFfieldExist($tablename, str_replace(':', '', $reader->name)) == 1){

                $name = str_replace(':', '', $reader->name);
                $reader->read();
                $val = (string)$reader->value;

                    $arrayfield[] = $name;
                    $arrayvalue[] = $val;

                echo $name.' --> '.$val.'<hr>';
            }

        }
    }

and work nice, here the function CheckIFfieldExist

function CheckIFfieldExist($table, $field){ 
    $var = 0;
    $result = mysql_query("SHOW COLUMNS FROM $table LIKE '$field'");
    $exists = (mysql_num_rows($result))?TRUE:FALSE;
    if($exists) {$var = 1;}
    return $var;
}

here u can see my XML xml example

i have 2 issues first i would read only a node called "AttributeSets" and all the nodes inside it

second how can i know when reading ending a node and skip to next?

Upvotes: 0

Views: 396

Answers (1)

u_mulder
u_mulder

Reputation: 54841

First: $reader also has name property: $reader->name which holds the name of the node. Check it and do what you need.

Second: There's a special constant XMLReader::END_ELEMENT. Check if $reader->nodeType equals to it and do what you need. Of course you can use it with checking name of the node.

if ($reader->nodeType === XMLReader::END_ELEMENT && $reader->name === 'someTag') {
    // do stuff
}

Upvotes: 1

Related Questions