Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post

    Parsing XML With PHP

    I have a response in XML that I need to parse out, and I'm using the simplexml_load_string function, but I'm not understanding how to isolate a particular value in the response.

    Here is a sample of the XML response:

    Code:
    <XMLRESPONSE>
    <SignMsgsRs>
    <SignonDesktopRs statusCode="0" statusSeverity="INFO">
    <ServerDateTime>2013-05-26T20:58:58</ServerDateTime>
    <SessionTicket>V1-Q0c9yykzqypvjqo9af2ccq:71327xy939</SessionTicket>
    </SignonDesktopRs>
    </SignMsgsRs>
    </XMLRESPONSE>
    Then after loading it into the simplexml_load_string, I can use print_r and see the above structure. But I need to get the value of the SessionTicket, so I did the following and it resulted in no output in any of the three echo statements:

    PHP Code:
    $xml simplexml_load_string($response);
    echo 
    $xml->SessionTicket;
    echo 
    $xml->SignMsgsRs->SessionTicket;
    echo 
    $xml->XMLRESPONSE->SignMsgsRs->SessionTicket
    Am I doing this wrong or is there just a better way to get a specific value of an XML string?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I'm thinking it's like this ... untested ...

    See what this does:

    foreach($xml->XMLRESPONSE->SignMsgsRs as $item) {
    echo $item->SessionTicket;
    }

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mlseim View Post
    I'm thinking it's like this ... untested ...

    See what this does:

    foreach($xml->XMLRESPONSE->SignMsgsRs as $item) {
    echo $item->SessionTicket;
    }
    I tried this out and it resulted in the following error:

    Warning: Invalid argument supplied for foreach() in /home/content/78/6009954/html/test.php on line 83

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,259
    Thanks
    10
    Thanked 277 Times in 276 Posts
    PHP Code:
    echo "<p>".$xml->SignMsgsRs->SignonDesktopRs->SessionTicket."</p>"
    Dave

  • Users who have thanked tracknut for this post:

    cgdtalent (05-27-2013)

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,493
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This is it ... I tested it:

    foreach($xml->SignMsgsRs->SignonDesktopRs as $item) {
    echo $item->SessionTicket;
    }

    I didn't notice the SignonDesktopRS at first. That is what I had missing in my first example.

  • Users who have thanked mlseim for this post:

    cgdtalent (05-27-2013)

  • #6
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Quote Originally Posted by tracknut View Post
    PHP Code:
    echo "<p>".$xml->SignMsgsRs->SignonDesktopRs->SessionTicket."</p>"
    Dave

    Thanks, that worked great!

  • #7
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mlseim View Post
    This is it ... I tested it:

    foreach($xml->SignMsgsRs->SignonDesktopRs as $item) {
    echo $item->SessionTicket;
    }

    I didn't notice the SignonDesktopRS at first. That is what I had missing in my first example.
    Thanks, this also worked great!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •