Thursday 30 August 2018

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.



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

No comments:

Post a Comment