Process XML data

Discussion in 'PHP' started by ycc, Sep 6, 2010.

  1. #1
    I haven't got the hang on XML.

    I use AJAX/PHP to retreive data that sometimes come as XML. For example like this:
    http://ipinfodb.com/ip_query.php?ip=66.249.68.163&timezone=false

    I want to use it in a webpage. I have come to think of two ways of processing the data:

    1. Make a style sheet. Unwanted data can for example be put as invisible
    .regioncode {position:absolute; visibility: hidden;} (I haven't tried in detail)

    2. use PHP preg_replace to remove unwanted portions
    preg-replace('/<regioncode>.*?</regioncode>/', '')

    It seems I haven't really understood the smart thing about XML, please help me take care of my XML data, thanks.

    I have seen similar questions in the XML forum, but I haven't really understood the answers. Sorry if I missed something.
     
    ycc, Sep 6, 2010 IP
  2. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #2
    You can use php's simplexml_load_string() function to read the xml elements
    Assuming you are capturing the output in $data variable, you can use the following code

    $xml = simplexml_load_string($data);
    $ip_address = $xml->Ip;
    $country_code = $xml->CountryCode;

    and so on... for other elements
     
    krishmk, Sep 6, 2010 IP
    ycc likes this.
  3. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you krishmk.

    Your reply was exactly what I was looking for. Works perfectly, of course.

    This kind of forums can really be helpful. Still find it amazing to get immediate help from someone far away. I visited Chennai last December. Very interesting place. "It is a flat world."
     
    ycc, Sep 6, 2010 IP
  4. ryantetek

    ryantetek Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    But remember, simplexml_load_string() is only available in PHP5 so you have to make a workaround if your server is below 5.
     
    ryantetek, Sep 6, 2010 IP
  5. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for reminding. I upgraded tp PHP5 some 6 months ago. A few programs then broke but could be repaired quickly, I don't remember which programs. But the reason to upgrade that time was to use some routines that directly parsed a web-page (f.ex. obtained by curl) into its Document Object Model nodes. (This process somehow resembles the present problem, I suddenly realized.) It seems PHP5 has come with some new good stuff?
     
    Last edited: Sep 7, 2010
    ycc, Sep 7, 2010 IP
  6. cubicaaron

    cubicaaron Guest

    Messages:
    104
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You could also use XSLT shylesheets to render XML Files as useable text on your pages, however the solution above is probably more use to you in this instance.
     
    cubicaaron, Sep 7, 2010 IP