0

I'm trying to use xpath in conjunction with DOMDocument to try and parse my xml and insert into a table.

All my variables are inserting correctly other than $halftimescore - why is this?

Here is my code:

<?php

  define('INCLUDE_CHECK',true);
  require 'db.class.php';

  $dom = new DOMDocument();
  $dom ->load('main.xml');

  $xpath = new DOMXPath($dom);
  $queryResult = $xpath->query('//live/Match/Results/Result[@name="HT"]');
  foreach($queryResult as $resulty) {
    $halftimescore=$resulty->getAttribute("value");
  }      


  $Match = $dom->getElementsByTagName("Match"); 
  foreach ($Match as $match) {

    $matchid = $match->getAttribute("id");
    $home = $match->getElementsByTagName("Home");
    $hometeam = $home->item(0)->getAttribute("name");
    $homeid = $home->item(0)->getAttribute("id");
    $away = $match->getElementsByTagName("Away");
    $awayid = $away->item(0)->getAttribute("id");
    $awayteam = $away->item(0)->getAttribute("name");

    $leaguename = $match->getElementsByTagName("league");
    $league = $leaguename->item(0)->nodeValue;
    $leagueid = $leaguename->item(0)->getAttribute("id");


    foreach ($match->getElementsByTagName('Result') as $result) {
      $resulttype = $result->getAttribute("name");
      $score = $result->getAttribute("value");
      $scoreid = $result->getAttribute("value");
    }

    mysql_query("
      INSERT INTO blabla
        (home_team, match_id, ht_score, away_team)
      VALUES
        ('".$hometeam."', '".$matchid."', '".$halftimescore."', '".$awayteam."')
    ");

  }
8
  • ...and var_dump($halftimescore); shows...? Also please show the input XML document. Commented Dec 11, 2012 at 17:30
  • string(3) "1-3" string(3) "1-3" string(3) "1-3" Commented Dec 11, 2012 at 17:32
  • Where did you place the var_dump() line that showed that result? (at which line in the code) Commented Dec 11, 2012 at 17:33
  • line 42. Would you like me to include my xml in the post? Commented Dec 11, 2012 at 17:37
  • No, if you have a value then it should probably work - what gets inserted instead of the value (what do you see in the db after the operation)? Also, do you get any error messages? Commented Dec 11, 2012 at 17:39

1 Answer 1

1

Because you populated $halftimescore outside the main loop, in a loop of its own, it will only have one value (the last value) because each iteration overwrites the previous.

What you need to do instead is run the XPath query within the main loop, with a base node of the current node, like this:

  // ...

  $xpath = new DOMXPath($dom);
  /*
  Remove these lines from here...
  $queryResult = $xpath->query('//live/Match/Results/Result[@name="HT"]');
  foreach($queryResult as $resulty) {
    $halftimescore=$resulty->getAttribute("value");
  }
  */


  $Match = $dom->getElementsByTagName("Match"); 
  foreach ($Match as $match) {

    // and do the query here instead:
    $result = $xpath->query('./Results/Result[@name="HT"]', $match);
    if ($result->length < 1) {
      // handle this error - the node was not found
    }
    $halftimescore = $result->item(0)->getAttribute("value");

  // ...
Sign up to request clarification or add additional context in comments.

3 Comments

Ah that looks right to me.. but is coming up with this error: Call to undefined method DOMNodeList::getAttribute()
@AndrewCharlton Sorry my bad. The method chaining's probably not great anyway, give me 1 sec will edit
It works! Thanks for taking time to answer this :) I best do some more research on the subject

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.