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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts

    xml feed into php/MYSQL listings site - noob question(s)

    Hello all

    i have a listings site that runs php from mysql results, all good

    but now I want to take an xml feed from a client with hundreds of live listings

    all new to me and i want to be sure I have the big picture right before plunging in

    am I right to think that I give them an XML template which is really just a list of the fields I need and the tags for each, so more or less what I'd get from my current MYSQL query, data and markup wise?

    So when my regular MYSQL query runs, I also pull the xml data from their site (or from a cache of that feed), parse it and then merge it with the array from MYSQL, on the fly server side, and then carry on as before?

    Is it that simple and are there any noob dos and don'ts?

    thanks
    Tynan

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    I'd advise you to use JSON -newer data format which is much easier to use.

    But if you still want to use XML,
    PHP has a very robust XML parser. It has many methods that will make XML parsing a breeze.


    Check out phpfreaks' xml tutorial.

    http://www.phpfreaks.com/tutorial/handling-xml-data

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi and thanks

    noted but client is asking to use XML so I guess I'm stuck with what he wants to feed

    Are my airey and vague assumptions correct and workable though?

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    Yeah. That's doable. Done that a lot before.

    Although your job is also to advise your client. Make him see the light on JSON. Unless he has a very specific reason to use XML like that is what their feeds are made using.

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, have skimmed that tutorial on the link and it looks a doddle which is nice

    I'll get this one under my belt before telling people doing it already what to do I think

    Thanks for your help, I may of course be back in due course

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And here indeed I am again

    I've quite happily managed to parse XML feeds and access the data and loop through it etc etc

    But I'm now trying to build a php array from the xml that I can then merge with an existing array from a local MYSQL query

    When my doubtless simpleton code as below runs

    PHP Code:
    $xml simplexml_load_file('http://www.xml2u.com/Xml/Blue%20Square_1314/2389_Default.xml');
    $Properties $xml->xpath('Clients/Client/properties/Property');
    //$xml = json_decode(json_encode((array) simplexml_load_file($xml)), 1);
    $x=0;
    foreach (
    $Properties as $Property)//immoblier

    {

    $Ref $Property->category;

    $Region $Property->Address->region;
    $Department $Property->Address->subRegion;
    $Town $Property->Address->location;
    $ClassifiedText $Property->Description->description;
    $AdTitle $Property->Description->title;
    $Area $Property->Description->floorSize;
    $Land $Property->PlotSize->plotSize;
    $Photo1 $Property->images->image[1]->image;
    $Photo2 $Property->images->image[2]->image;
    $Photo3 $Property->images->image[3]->image;

    $arrayBS=array(Ref=>$Ref,Region=>$Region,Department=>$Department,Town=>$Town,ClasifiedText=>$ClassifiedText,AdTitle=>$AdTitle,Area=>$Area,Land=>$Land,Photo1=>$Photo1,Photo2=>$Photo2,Photo3=>$Photo3);
    $x++;
    }
    print_r($arrayBS); 
    What on earth am I not doing right?

    The xml file is as below in case it's not normal

    http://www.xml2u.com/Xml/Blue%20Squa...89_Default.xml

    yesterday I could create an array, today only a error

    Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "" in /home/rauxvsuj/public_html/ClassifiedXML.php on line 175

    sorry, I'm frazzled and confused and would love someone to squint at the xml feed and point in the right direction to create a nice and easy php array, from there I can do it

  • #7
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bah, I think I'm there after posting it here, ain't that so often the way, the below copied from somewhere gives me a clean array

    PHP Code:
    <?php
    $countries 
    = array();
    $dom = new DOMDocument();
    $dom->load('http://www.xml2u.com/Xml/Blue%20Square_1314/2389_Default.xml');
    foreach (
    $dom->getElementsByTagName('properties') as $Property) {
      
    $country_name $Property->getElementsByTagName('propertyid')->item(0)->textContent;
      
    $country_code $Property->getElementsByTagName('category')->item(0)->textContent;
      
    $countries[$country_name] = $country_name;
      
    $countries[$country_code] = $country_code;
      
    print_r($countries);
    }
    ?>

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so far so good, I can load everything into the php array except a list of images that are numbered and use the tag name 'image' nested inside another tag name of 'image', I can't get into there, I presume because the DOM code finds the outer image tag? Google not helping specifically so far

    Code:
    <images>
    <image number="1">
    <image>
    http://www.emulis.net/shared/depot/3...ges/664567.jpg
    </image>
    <image number="2">
    <image>http://www.emulis.net/shared/depot/3...ges/664565.jpg
    </image>
    </images>

  • #9
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tynan View Post
    so far so good, I can load everything into the php array except a list of images that are numbered and use the tag name 'image' nested inside another tag name of 'image', I can't get into there, I presume because the DOM code finds the outer image tag? Google not helping specifically so far

    Code:
    <images>
    <image number="1">
    <image>
    http://www.emulis.net/shared/depot/3...ges/664567.jpg
    </image>
    <image number="2">
    <image>http://www.emulis.net/shared/depot/3...ges/664565.jpg
    </image>
    </images>
    hmpf, I can get the image links with the following rather obvious lines

    PHP Code:
    $Photo1=$Property->getElementsByTagName('image')->item(1)->textContent
    but it starts going wrong when the outer image tag has a child of 'alt' as below

    Code:
    <images>
    <image number="1">
    <image>http://www.emulis.net/shared/depot/3168/biens/149506/images/627783.jpg</image><alttext/></image>
    <image number="2"><image>http://www.emulis.net/shared/depot/3168/biens/149506/images/627794.jpg</image>
    <alttext>Ext</alttext>
    </images>
    I get the alt text suffixed to the image text and I get the previous link instead of the third when I ask for


    PHP Code:
    $Photo3=$Property->getElementsByTagName('image')->item(3)->textContent
    if you can image a third one

  • #10
    Regular Coder
    Join Date
    Oct 2004
    Location
    London E4 UK
    Posts
    320
    Thanks
    0
    Thanked 0 Times in 0 Posts
    found a way around this, apologies for anyone reading my rather frenetic and unclear posts above


  •  

    Posting Permissions

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