SimpleXML and colons?

Discussion in 'PHP' started by alex6, Apr 6, 2008.

  1. #1
    Hey there!

    I'm working on an application that uses the White Pages API, which returns all queried data in XML. I'm trying to use SimpleXML to read the query status code, which is returned in the second line of the code below ("wp:code="No Data Found""). I want my application to do different things based on the code returned (ie; "No Data Found" = show error, "Data Found" = show listing, etc.), but I'm having trouble reading this part of the XML... I'm trying to use $simplexml->wp:wp->wp:result->attributes(); but the colons keep getting in the way and I get an error:

    Any ideas?

    <wp:wp>
    <wp:result wp:type="success" wp:message="The search did not find results" wp:code="No Data Found"/>
    −
    <wp:meta>
    <wp:linkexpiration>2008-04-11</wp:linkexpiration>
    <wp:recordrange wp:lastrecord="0" wp:firstrecord="0" wp:totalavailable="0"/>
    <wp:apiversion>1.0</wp:apiversion>
    −
    <wp:searchlinks>
    <wp:link wp:linktext="Whitepages.com" wp:type="homepage">http://www.whitepages.com/*****/</wp:link>
    −
    <wp:link wp:linktext="Link to this api call" wp:type="self">
    http://api.whitepages.com/reverse_phone/1.0/?phone=**********;api_key=*****************************
    </wp:link>
    </wp:searchlinks>
    </wp:meta>
    </wp:wp>
    Code (markup):

     
    alex6, Apr 6, 2008 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can't access it that way. While simplexml is awesome, that's one of the limitations.

    See if this works for a workaround.
     
    decepti0n, Apr 6, 2008 IP
  3. alex6

    alex6 Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Then how can I access it? What other libraries can I use and how? Can you post an example?

    Thanks for the help! :)
     
    alex6, Apr 6, 2008 IP
  4. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, I just worked this out about 5 minutes ago, so we're in the same boat since neither of us really know. This seems to work though:

    $string = <<< XML
    <?xml version="1.0"?>
    <root xmlns:wp="http://example.com/xmlns/">
    	<wp:wp>
    		<wp:result wp:type="success" wp:message="The search did not find results" wp:code="No Data Found"/>
    		<wp:meta>
    			<wp:linkexpiration>2008-04-11</wp:linkexpiration>
    			<wp:recordrange wp:lastrecord="0" wp:firstrecord="0" wp:totalavailable="0"/>
    			<wp:apiversion>1.0</wp:apiversion>
    			<wp:searchlinks>
    				<wp:link wp:linktext="Whitepages.com" wp:type="homepage">http://www.whitepages.com/*****/</wp:link>
    				<wp:link wp:linktext="Link to this api call" wp:type="self">
    				http://api.whitepages.com/reverse_phone/1.0/?phone=**********;api_key=*****************************
    				</wp:link>
    			</wp:searchlinks>
    		</wp:meta>
    	</wp:wp>
    </root>
    XML;
    
    $sxml = simplexml_load_string($string, null, null, 'wp', true);
    echo $sxml->wp->result['message'];
    PHP:
    The code you posted up there ^^ should have a bit extra around it (or it wouldn't be valid code).

    The simplexml_load_string parameters:
    1. $string is the string obviously
    2. null because we don't want to apply $sxml to a class (i dont really know what that does)
    3. null for no libxml options
    4. 'wp' for our namespace
    5. true for something, just rely on it because it only works with it set. There's no php.net docs on it

    Try it out, it should hopefully work :)
     
    decepti0n, Apr 6, 2008 IP