简体   繁体   中英

Checking XML attribute value

I'm exporting data from XML file in order to update my database. I'm using the following loop:

foreach ($xml->removed->idof as $idof) {
  if ($idof > 0) {
    mysql_query("delete from wp_posts WHERE import_id = '$idof'");
    echo $idof;
  }
}

My data structure looks like this:

<removed>
<idof mlssta='4'>0</idof>
<idof mlssta='6'>60370</idof>
<idof mlssta='14'>150370</idof>
<idof mlssta='6'>150671</idof>
...
</removed>

I need to change the if condition in order to check the value of mlstaa attribute, ie if ($idof > 0 && mlstaa != 6) .

I just don't know how extract it. Really would appreciate your help.

The use of $xml variable looks like it is loaded with SimpleXML extension. Suppose it is initialized like this:

$xml = simplexml_load_file('some-file.xml');

Then you just fetch attributes with attributes() and check if the attribute is in the returned array:

foreach ($xml as $idof) {
  $attr = $idof->attributes();
  if ($attr && $attr['mlssta'] != 6) {
    // remove it here
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM