0

So I've been able to apply some code online from previous discussions to a project that I'm working on however, now that there are multiple xml elements to grab I cannot seem to figure out how.

The xml structure is set up like this: ``

<DEPTId id = "1234">
  <GROUP_DESCRIPTION>Something</GROUP_DESCRIPTION>
  <DESCIPTION>Some department</DESCRIPTION>
  <SOFTWARE>piece 1<SOFTWARE>
  <SOFTWARE>DSFAON</SOFTWARE>
  <SOFTWARE>asdvn</SOFTWARE>
<DEPTId>

I have tried to either reformat the structure to be SOFTWARE1, SOFTWARE2, .. but I felt as though one element should suffice as there can be many pieces of software, and couldn't find a way to iterate across each element. However the code I have only prints out the SOFTWARE elements text as one string, and not separate checkboxes which I want.

My JavaScript is below. It checks their group and department from the html page, and if it is equal to the xml it will append the listed software to the table on the page.

function defaultSoftware() {
  $("#defaultSoftwareList").empty();

  $.ajax({
    type: "GET",
    url: "blahblah.xml",
    contentType: "charset=utf-8",
    dataType: "xml",
    async: true,
    success: parsePersonaXML
  });

  function parsePersonaXML(xml) {

    $(xml).find("DEPTId").each(function() {
      var htmlDepartment = $("#b1Department").val();
      var htmlGroup = $("#techpersonaGroup").val();
      var xmlGroup = $(this).find("GROUP_DESCRIPTION").text();
      var xmlDepartment = $(this).find("DESCRIPTION").text();
      

      if (htmlDepartment == xmlDepartment && htmlGroup == xmlGroup)
        $(this).find("SOFTWARE").each(function() {
            var sd = ("SOFTWARE").text();
            $("#techpersonaList").append('<input type="checkbox" checked /> ' + sd  + '<br />');
       });
    });
  }
}

If anyone knows what I am doing wrong or how I can change it your help would be much appreciated!

Chris
  • 3
  • 2
  • `$(this).find("SOFTWARE")` would return all of the SOFTWARE nodes allowing you to iterate them, but if you apply `.text()` to it you get a single string containing everything within all SOFTWARE nodes. So, within your if, iterate the nodes and for each single node use the `.text()` method (not on all of the nodes at once). – James Mar 09 '18 at 13:27
  • i highly highly highly recommend reading this. Read all of the answers to because it talks about performance & different methods. when i read this code you repurposed i immediately thought of this: [Find object by ID in an array of JS objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects/35398031#35398031) –  Mar 09 '18 at 13:31
  • Thank you both for your info. James I will try out your solution, and I think that should work... DieNerd thank you for the link I will go through this thread it does look like alot of information that is what I am looking for! – Chris Mar 09 '18 at 22:27

0 Answers0