doniyor
doniyor

Reputation: 37904

DOMdocument search for tag

i am trying to do this:

i have several thousand xml files, i am reading them, and i am looking for special text inside an xml with specific tag, but those tags which are having the text i need, are different. what i did till now is this:

$xml_filename = "xml/".$anzeigen_id.".xml";
    $dom = new DOMDocument();
    $dom->load($xml_filename);
    $value = $dom->getElementsByTagName('FormattedPositionDescription');
    foreach($value as $v){
        $text = $v->getElementsByTagName('Value');
        foreach($text as $t){
            $anzeige_txt = $t->nodeValue;
            $anzeige_txt = utf8_decode($anzeige_txt);
            $anzeige_txt = mysql_real_escape_string($anzeige_txt);
            echo $anzeige_txt;
            $sql = "INSERT INTO joinvision_anzeige(`firmen_id`,`anzeige_id`,`anzeige_txt`) VALUES ('$firma_id','$anzeigen_id','$anzeige_txt')";
            $sql_inserted = mysql_query($sql);
            if($sql_inserted){
                echo "'$anzeigen_id' from $xml_filename inserted<br />";
            }else{
                echo mysql_errno() . ": " . mysql_error() . "\n";
            }
        }
    }

now what i need to do is this:

look for FormattedPositionDescription in xml and if there is not this tag there, then look for anothertag in that same xml file..

how can i do this, thanks for help in advance

Upvotes: 0

Views: 65

Answers (1)

MrCode
MrCode

Reputation: 64536

Just check the length property of the DOMNodeList:

$value = $dom->getElementsByTagName('FormattedPositionDescription');

if($value->length > 0)
{
    // found some FormattedPositionDescription
}
else
{
    // didn't find any FormattedPositionDescription, so look for anothertag
    $list = $dom->getElementsByTagName('anothertag');
}

Upvotes: 2

Related Questions