BobR
BobR

Reputation: 85

Parsing fields and values from an XML file using PHP

I am trying to find a way to get fields and values reading a file. The file is constructed similar to an XML so its like this..

<tag field1="value1" field2="value2" field3="value3" .... />
<tag2 field5="value5" field6="value6" field7="value7" ... />

..
<tagn .... />

If I only interested in only one specific tag (e.g. the first one), how can I easily get the fields and values from that line ?

Here is what I have managed to do, but there may be an easier way since the file is XML constructed ?

function string2KeyedArray($string, $delimiter = '" ', $kv = '=') {
  if ($a = explode($delimiter, $string)) { // create parts separated by a " + space
    foreach ($a as $s) { // each part
// Removing the known tag name
      $s = str_replace("tag ","",$s);
//removing the starting < and the ending />
      $s = str_replace("<","",$s);
      $s = str_replace(">","",$s);
      $s = str_replace("/","",$s);
//removing the " from the value
      $s = str_replace("\"","",$s);
      if (strpos($s,"=")) {
        if ($pos = strpos($s, $kv)) { // key/value delimiter
          $ka[trim(substr($s, 0, $pos))] = trim(substr($s, $pos + strlen($kv)));
        } else { // key delimiter not found
          $ka[] = trim($s);
        }
      }
    }
    return $ka;
  }
}

$string ='<tag field1="value1" field2="value2" field3="value3" />'
$fields = string2KeyedArray($string);

//Which returns what I am looking for

Upvotes: 2

Views: 158

Answers (2)

BobR
BobR

Reputation: 85

Sorry for the late reply. The file i wanted to read was indeed an XML file and i used XML Parser at the end to store in the database. Thanks for the heads up.

Upvotes: 1

Kurt Leadley
Kurt Leadley

Reputation: 503

I would use DomDocument. I had a similar issue once. Look at my code example Here at the bottom of the thread. Check out the link to the XML file too. XML FIle. "team-standing" would be "tag" and "name", "wins", "losses" would be "field1", "field2", "field3" in your case.

$xmlDoc = new DOMDocument();
            $xmlDoc->load('http://www.tsn.ca/datafiles/XML/NHL/standings.xml');
            $searchNode = $xmlDoc->getElementsByTagName( "team-standing" ); 
            foreach ($searchNode as $searchNode) {
                $teamID = $searchNode->getAttribute('id');
                $name = $searchNode->getAttribute('name');
                $wins = $searchNode->getAttribute('wins');
                $losses = $searchNode->getAttribute('losses');
                $ot = $searchNode->getAttribute('overtime');
                $points = $searchNode->getAttribute('points');
                $goalsFor = $searchNode->getAttribute('goalsFor');
                $goalsAgainst = $searchNode->getAttribute('goalsAgainst');
                $confID = $searchNode->getAttribute('conf-id');
                $divID = $searchNode->getAttribute('division-id');

                $query = "INSERT INTO standings ('teamid','confid','divid','name','wins','losses','otl','pts','gf','ga')
                          VALUES ('$teamID','$confID','$divID','$name','$wins','$losses','$ot','$points','$goalsFor','$goalsAgainst')";
                $result= $db->query($query);
            }

Upvotes: 1

Related Questions