Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Filter Parsed XML data by Element Content [jQuery]

So I have a xml housing property feed [on a WordPress site] currently that is pretty simple, it just gathers the fields i want to show and displays that as a list [pretty normal stuff] But i now need to be able make two lists, one that shows only sold properties, and one does not show sold properties. Currently my code is as follows:

jQuery(function( $ ){

$(document).ready(function()
{
    $.ajax({
        type: "GET",
        url: "/properties2.xml",
        dataType: "xml",
        success: parseXml
    });
});

function parseXml(xml)
{
    $("#xmlmain").html("<div id='content' data-role='listview' data-inset='true'></div>");
    $(xml).find("property").each(function() {
        $("#content").append("<div class='xmlwrapper'><div class='xmlleft'><img src='"+$(this).find('[name="Photo 1"]').text()+"'/></div><div class='xmlright'><h2>"+$(this).find("advert_heading").text()+"</h2><p class='price'>"+$(this).find
        ("price_text").text()+"</p><p class='priority'>"+$(this).find("priority").text()+"</p><p>"+$(this).find("main_advert").text()+"</p><a href='"+$(this).find("web_link").text()+"' target='_blank'>VIEW > </a></div></div>");
    });
}
});

Im pretty novice at Javascript and Jquery so im really not sure how i go about filtering the lists to exclude sold and only include sold properties. How do i adapt/filter this to get the required result? I tried some attemps with filter(); function but it just kept stopping the feed from displaying at all.

This was the snippet/example i was trying to incorporate/work with:

var getTextNodesIn = function(el) {
return $(el).find(":not(iframe)").addBack().contents().filter(function() {
    return this.nodeType == 3;
    });
};

getTextNodesIn(el);

The data i need to use is in the Priority Field shown below. Here is an extract from the xml feed:

<properties>
<property reference="MR139">
    <instructedDate>06/08/2018 17:07:05</instructedDate>
    <price_text>£600,000</price_text>
    <numeric_price>600000.0000</numeric_price>

    <priority>On Market</priority>

    <advert_heading>house for sale</advert_heading>
    <main_advert>some text about the property</main_advert>
    <web_link>www.example.com</web_link>
    <property_style>Detached</property_style>
    <property_reference>111111</property_reference>
    <newHome>NO</newHome>
    <noChain>NO</noChain>
    <furnished>Unknown</furnished>
    <currency>GBP</currency>
    <featuredProperty>NO</featuredProperty>
    <pictures>
        <picture name="Photo 1" lastchanged="2018-08-06T15:44:48.5800534Z">
            <filename>example.jpg</filename>
        </picture>
    </pictures>
</property>
</properties>

[The text in the priority field for sold properties will either be "Sold" or "Sold STC" if that makes a difference.]

Any help would be much appreciated, even if its just pointing me to resources i can use that are relevant to my problem. My searches seem to turn up unrelated information, potentially due to me wording things wrong due to not knowing the terminology properly.

like image 397
Moose Avatar asked Mar 06 '23 14:03

Moose


2 Answers

You can check the value of priority in your each method using startsWith method like below.

$(xml).find("property").each(function() {
    var priority = $(this).find("priority").text();

    if(priority.startsWith('Sold')) {
        //for sold properties
    } else {
        //for unsold properties
    }
});
like image 155
Ibrahim Khan Avatar answered Mar 17 '23 21:03

Ibrahim Khan


Good luck dude.. it seems the community dont use xml in real life. I ve researched for days now and cant get even get a straight answer to storing the xml in a database or using xquery to get the data i need from the .xml feed directly... to then find an answer on how a user could search by e.g. price and display property in price order seems light years away it seems...

if your using wordpress it looks like property hive plugin might be your best bet... cost £150 but all searching is done for you.

like image 43
DonQuery Avatar answered Mar 17 '23 21:03

DonQuery