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
    92
    Thanks
    21
    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. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,079
    Thanks
    10
    Thanked 1,158 Times in 1,149 Posts
    I'm thinking it's like this ... untested ...

    See what this does:

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

  3. #3
    New Coder
    Join Date
    Jan 2013
    Posts
    92
    Thanks
    21
    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. #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,872
    Thanks
    12
    Thanked 401 Times in 398 Posts
    PHP Code:
    echo "<p>".$xml->SignMsgsRs->SignonDesktopRs->SessionTicket."</p>"
    Dave

  5. Users who have thanked tracknut for this post:

    cgdtalent (05-27-2013)

  6. #5
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,079
    Thanks
    10
    Thanked 1,158 Times in 1,149 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.

  7. Users who have thanked mlseim for this post:

    cgdtalent (05-27-2013)

  8. #6
    New Coder
    Join Date
    Jan 2013
    Posts
    92
    Thanks
    21
    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!

  9. #7
    New Coder
    Join Date
    Jan 2013
    Posts
    92
    Thanks
    21
    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
  •