0

I have an xml which is of the below format:

Xml:
<JobsArray>
<TechJobs>
 <JobsAtSite>
  <SiteJob>
    <JobId>100</JobId>
    <Name>1bc</Name>
  </SiteJob>
  <SiteJob>
    <JobId>101</JobId>
    <Name>abc</Name>
  </SiteJob>
  <SiteJob>
    <JobId>102</JobId>
    <Name>xbc</Name>
  </SiteJob>
 </JobsAtSite>
<TechJobSite>
  <JobsAtSite>
   <SiteJob>
    <JobId>200</JobId>
    <Name>1bc</Name>
   </SiteJob>
  </JobsAtSite>
</TechJobSite>
</JobsArray>

I want those jobs where the JobId is not equal to 100 for the same node. So the output should be in the below format:

<TechJobs>
 <JobsAtSite>
  <SiteJob>
    <JobId>101</JobId>
    <Name>abc</Name>
  </SiteJob>
  <SiteJob>
    <JobId>102</JobId>
    <Name>xbc</Name>
  </SiteJob>
 </JobsAtSite>
<TechJobSite>

This is the workaround which i had, but it is not giving me the expected result.

$(xml).find("SiteJob").filter(function () {
    return $(this).find("JobId").text().toLowerCase() !== "100";
}).each(function () {
    console.log($(this).parent());
});

Any fixes for this. Thanks.

8
  • node name is JobId but you use 'jobID', typo? Commented May 13, 2014 at 11:06
  • Yes typo, it should be JobId, i will edit that above. Commented May 13, 2014 at 11:08
  • No, it doesn't, its just a typo here. The code is not giving the expected result. Commented May 13, 2014 at 11:09
  • seems to work fine: jsfiddle.net/rnG9M, I get only 3 results... Commented May 13, 2014 at 11:14
  • What kind of filtering do you want? You are now only removing anything that has JobId 100 from the list... Commented May 13, 2014 at 11:17

1 Answer 1

1

You could try this:

$(xml).find("SiteJob:contains('100')").filter(function () {
    return true;
}).parent().find("SiteJob").filter(function () {
    return $(this).find("JobId").text().toLowerCase() !== "100";
}).each(function () {
    console.log($(this).find("JobId").text());
});

jsfiddle DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.