[RESOLVED] Simplexml_load_string not parsing XML
Results 1 to 10 of 10

Thread: [RESOLVED] Simplexml_load_string not parsing XML

  1. #1
    Member
    Join Date
    May 2004
    Posts
    66

    resolved [RESOLVED] Simplexml_load_string not parsing XML

    Hello,

    I think this is an error with the XML that's being produced, because visually they look different when I load them into Chrome. Here's the site that I'm having issues loading:

    http://www.webservicex.net/airport.a...irportCode=MSP

    In my php code, I'm using something like this:

    Code:
        public function load() {
            if($this->airport) {
                $url = "http://www.webservicex.net/airport.asmx/getAirportInformationByAirportCode?airportCode={$this->airport}";
    
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
                $result = curl_exec($ch);
    
                curl_close($ch);
    
                $this->xmlobj = simplexml_load_string($result);
            }
        }
    
        public function __get($name) {
            return $this->xmlobj->Table->$name;
        }
    I have the same code from the same provider, with a different XML request that works just fine:

    http://www.webservicex.net/uszip.asm...IP?USZip=97219

    The difference that I see is that everything is duplicated on the first link. I'm wondering if that's the issue because when I go looking for GMToffset, there are two that qualify and it doesn't know what to do. Is that a valid assumption? Is there an easy way to work around that? When I echo out the XML strings of these two responses they look like this:

    Code:
    GetAirportInfo Object
    (
        [airport] => MSP
        [xmlobj] => SimpleXMLElement Object
            (
                [0] => 
      MSPMINNEAPOLIS INTLUnited StatesUS6361000084144540N93130W
    
      MSPMINNEAPOLIS INTLUnited StatesUS6361000084144540N93130W
    
    
            )
    
    )
    Code:
    GetInfoByZIP Object
    (
        [zip] => 97219
        [xmlobj] => SimpleXMLElement Object
            (
                [Table] => SimpleXMLElement Object
                    (
                        [CITY] => Portland
                        [STATE] => OR
                        [ZIP] => 97219
                        [AREA_CODE] => 503
                        [TIME_ZONE] => P
                    )
    
            )
    
    )
    Obviously that second one looks way more like what I'm expecting and I can then move through the different nodes and pull out values like city, state, etc. This is my first foray into using curl and xml to retrieve data, so I'm learning as I go along. Thanks for any suggestions!

  2. #2
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,658
    Quote Originally Posted by scambro View Post
    because visually they look different when I load them into Chrome.
    Have you looked at the XML source for both documents?

  3. #3
    Member
    Join Date
    May 2004
    Posts
    66
    Quote Originally Posted by johanafm View Post
    Have you looked at the XML source for both documents?
    Not sure exactly what you mean. I did just look at the source in chrome (view source), but not sure what that told me. Thanks!

  4. #4
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,658
    Well, one should show things like < and > while the other shows things like <lt; and <gt;

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Quote Originally Posted by johanafm
    ...while the other shows things like <lt; and <gt;
    Wow, it's an XML document encoded as a string inside another XML document!
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Member
    Join Date
    May 2004
    Posts
    66
    I'm still struggling with this and am hopeful someone has an idea how I can parse this differently, or what is wrong with what I've currently tried. Thanks!

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    As I said, one of them is an XML document encoded as a string inside another XML document (one of the sillier things I've seen recently). So you'd need to parse the XML document to get the string inside, then send that string through the XML parser again.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  8. #8
    Member
    Join Date
    May 2004
    Posts
    66
    Quote Originally Posted by Weedpacket View Post
    As I said, one of them is an XML document encoded as a string inside another XML document (one of the sillier things I've seen recently). So you'd need to parse the XML document to get the string inside, then send that string through the XML parser again.
    Aaaah, that's not what I understood from your post. I should be able to mess around with that in about an hour. Thanks for the direction!

  9. #9
    Member
    Join Date
    May 2004
    Posts
    66
    Quote Originally Posted by scambro View Post
    Aaaah, that's not what I understood from your post. I should be able to mess around with that in about an hour. Thanks for the direction!
    That did it! Here's what I ended up doing, fyi:

    Code:
    $airportInfo = new GetAirportInfo(MSP);
    $airportInfo->xmlobj = simplexml_load_string($airportInfo->xmlobj,'SimpleXMLElement');
    Seems to work, although I am still confused why I see two sets of data in the XML string, but I'm hoping that they never conflict...

    Thanks!

  10. #10
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,658
    Considering how they screwed up their XML document to begin with, I wouldn't really count on anything... However, it is probably still safe to assume that both matching elements actually do match. I.e. the airport code of both elements will match the airport code you searched for. If you assume this is safe, then you can simply disregard either of the elements. Should you deem that you cannot trust the search functionality, you'd simply iterate over the result until you reach the end (no match) or find a matching airport code. And in this case, you should _always_ do this - even if only one airport would be returned.

    Well, you could always compare the airport code for the two elements. If

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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