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

BobR
  • 85
  • 6
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Nov 04 '15 at 21:10
  • 1
    yes, there is an easier way to do this. look at [DOMDocument->loadXML](http://php.net/manual/en/domdocument.loadxml.php) and [DOMXPATH query](http://php.net/manual/en/domxpath.query.php) – devlin carnate Nov 04 '15 at 21:16
  • "similar to an XML" or XML? This is important. If it is XML, use a parser like `SimpleXML` or `DOM`. – michi Nov 05 '15 at 11:30

2 Answers2

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);
            }
Community
  • 1
  • 1
Kurt Leadley
  • 513
  • 3
  • 20
  • How do you know OP intends to import into database? And how is your XML structure aligned to OP's post? – Parfait Nov 05 '15 at 00:50
  • 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'); – Kurt Leadley Nov 05 '15 at 02:01
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.

BobR
  • 85
  • 6