0

I have the following complex, not quite well-structured XML:

<event hometeam="K.Kanepi" awayteam="S.Lisicki" eventName="K.Kanepi-S.Lisicki" date="02/07/2013" time="14:05" datetime="2013-07-02T14:05:00+02:00">
    <subevent title="Match odds" dsymbol="MATCH_ODDS">
        <selection name="home" odds="3.1500" slipURL="URI/en/add-bet/3187-02072013,1"/>
        <selection name="away" odds="1.3500" slipURL="URI/en/add-bet/3187-02072013,2"/>
    </subevent>

I'm trying to parse it using this code:

$total_games = $xml->event->count();

for ($i = 0; $i < $total_games; $i++) {
    echo $xml->event[$i]->subevent.title;

$total games works correctly; however - when I try to parse subevent, I'm unsuccessful in getting the eventName, hometeam, date, etc.

6
  • 4
    Are you using DomDocument, SimpleXML or another library? Commented Jul 2, 2013 at 15:45
  • Appears to be SimpleXML by the syntax. Commented Jul 2, 2013 at 15:48
  • better try to fix the xml instead of tryign to force your DOM library to try and handle mal-formed input. If all it takes is appending </event> to that xml string to make it well-formed, then take that easy road. Commented Jul 2, 2013 at 15:50
  • It might be easier with xpath. Give it a read. Commented Jul 2, 2013 at 17:47
  • possible duplicate of Accessing @attribute from SimpleXML Commented Jul 3, 2013 at 3:40

1 Answer 1

5

To access attributes, you need to use array subscripting instead of class member syntax. This works for me using your XML as input:

<?
    $xml = simplexml_load_file('http://some.url.here/gimme.xml');

    $total_games = $xml->event->count();

    for ($i = 0; $i < $total_games; $i++) {
        echo $xml->event[$i]->subevent['title'];
        //       Note the syntax here ^^^^^^^^^
    }
?>

PHP's Basic SimpleXML usage page is a good resource in this case.

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

3 Comments

That's what I thought as well, but I'm not getting anything returned with that syntax?
Is the XML really malformed?
Here's the true location of the XML - I can't figure out how to parse it using simpleXML: odds.betgun.com/makeBB.php?id=WTA_WIMBLEDON

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.