2

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

3

2 Answers 2

1

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);
            }
Sign up to request clarification or add additional context in comments.

2 Comments

How do you know OP intends to import into database? And how is your XML structure aligned to OP's post?
He doesn't have to do anything with a database. This method will still get the values of the attributes. He can echo the values, store them in a database, whatever he wants to do with them. As for the structure of the xml, just click the link to the xml file I was using. Like I said "tag" = "team-standing" So $searchNode = $xmlDoc->getElementsByTagName( "tag" ); and do the same for the attributes of the tag ` $field1 = $searchNode->getAttribute('field1');
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.