SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems parsing XML

    I am sending some user data to an API which in return sends me a response in XML format. I use CURL to send the data.

    Code:
    - <response>
      <sm>OK</sm> 
      <lid>16819301</lid> 
      <ldaid>1185459</ldaid> 
      <nb>4</nb> 
      <amt>100.75</amt> 
    - <buyer>
      <bn>Buyer 1</bn> 
      <id>-1</id> 
      <lu>/images/ilitest/lp_test_1.gif</lu> 
      <lp>http://www.example.org/buyer1</lp> 
      <dsc>Test description for company 1</dsc> 
      <ph>800-555-0111</ph> 
      <em>buyer1@example.org</em> 
      </buyer>
    - <buyer>
      <bn>Buyer 2</bn> 
      <id>-1</id> 
      <lu>/images/ilitest/lp_test_2.gif</lu> 
      <lp>http://www.example.org/buyer2</lp> 
      <dsc>Test description for company 2</dsc> 
      <ph>800-555-0122</ph> 
      <em>buyer2@example.org</em> 
      </buyer>
    - <buyer>
      <bn>Buyer 3</bn> 
      <id>-1</id> 
      <lu>/images/ilitest/lp_test_3.gif</lu> 
      <lp>http://www.example.org/buyer3</lp> 
      <dsc>Test description for company 3</dsc> 
      <ph>800-555-0133</ph> 
      <em>buyer3@example.org</em> 
      </buyer>
    - <buyer>
      <bn>Buyer 4</bn> 
      <id>-1</id> 
      <lu>/images/ilitest/lp_test_4.gif</lu> 
      <lp>http://www.example.org/buyer4</lp> 
      <dsc>Test description for company 4</dsc> 
      <ph>800-555-0144</ph> 
      <em>buyer4@example.org</em> 
      </buyer>
      <sid /> 
      <cid /> 
      <mid /> 
      <tid /> 
      </response>
    However, I also see this header information:

    Code:
    string(1334) "HTTP/1.1 200 OK
    Server: Resin/3.0.23
    P3P: CP="CAO DSP COR CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT"
    Pragma: no-cache
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Cache-Control: no-cache
    Cache-Control: no-store
    Set-Cookie: JSESSIONID=abcoBg_CUGPxcbPTo1_bs; path=/
    Content-Type: text/xml
    Transfer-Encoding: chunked
    Date: Tue, 07 Apr 2009 04:27:44 GMT
    Now my problem is to parse the XML properly and then to display just the information about the 4 different buyers if the status is "OK" (or to display an error page if the status is not Ok.

    I spent the last 3 hours trying different code samples from many different websites, but this thing will just not work. Now I am completely drawing blank and need some advice on how to tackle this one and which way to go.

    Thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2008
    Location
    NC
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What language are you trying to handle the data in? php, perl, python? Parsing XML can be tricky, and each language handles it a little differently. For instance, python has an awesome XML parser called BeautifulSoup, but in php you kind of have to roll your own.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In php using SimpleXML

    PHP Code:
    <?php
    $response 
    simplexml_load_file('yourfile.xml'); //or perhaps simplexml_load_string() in your case
    if ($response->sm == 'OK') {
    foreach (
    $response->buyer as $buyer) {
    echo 
    $buyer->bn;
    echo 
    $buyer->id;
    echo 
    $buyer->lu;
    echo 
    $buyer->lp;
    echo 
    $buyer->dsc;
    echo 
    $buyer->ph;
    echo 
    $buyer->em;
    }
    }
    else
    echo 
    "no dam good";
    ?>

  4. #4
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice Job J. Thats just what I was looking for I think. I have a couple questions for you if you don't mind. Just testing on a local file, I had to remove the "if(response==ok)" part or it wouldn't work. A simple explanation of what that section checks for would be appreciated. Also my end result would be to receive a download feed using a url to get it and parse it out. I'll test it shortly but I wonder if I need to use a different function? All the other tips I've found use get_feed() but your solution is so much simpler.
    Thanks

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($response->sm == 'OK') just checks the <sm>OK</sm> field in your sample XML document. From your original post, I assumed that was what you meant.

    I don't know if simplexml_load_file will work for you, all you can do is try, probably depends on the data source. You can use simplexml_load_string if you're getting the xml data via curl.

    Not sure what get_feed() is, google shows it as a SimplePie function. SimplePie is meant for RSS/Atom feeds (I use it myself ) , not generic XML data like your sample file. Better to use SimpleXML for that.

  6. #6
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks J. I'm not the original poster, was just looking for solutions and found your post. Works great for the xml files so thanks. I can get the feed in XML so should work fine.
    Thanks for your help.


Bookmarks

Posting Permissions

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