2016-03-23 15 views
0

Meine XML-Datei sieht wie folgt aus -XML mit Filterung Parsen

<?xml version="1.0" encoding="UTF-8"?> 
    <prj:Flow xmlns:prj="url1" xmlns:com="url2" xmlns:ns2="url3" xmlns:con="url4" xmlns:ns0="url5" xmlns:ns1="url6" xmlns:ns3="url7"> 
    <prj:str> 
    <prj:layout comp="abcd"> 
     <prj:prop> 
     <prj:property name="Hardik" value="5000"/> 
     </prj:prop> 
    <prj:look> 
     <prj:lite name="bajaj"> 
     <prj:lite name="honda"> 
    </prj:look> 
    </prj:layout> 
<prj:layout comp="efgh"> 
     <prj:prop> 
     <prj:property name="Vipul" value="6000"/> 
     </prj:prop> 
    <prj:look> 
     <prj:lite name="yamaha"> 
     <prj:lite name="honda"> 
    </prj:look> 
    </prj:layout> 
    </prj:str> 
    </prj:Flow> 

Ich brauche diese XML zu analysieren, so dass ich die Kinder auf der Grundlage des comp Wert abrufen kann, dh wenn comp = „ABCD“, dann soll ich Zeige Bajaj und Honda in meiner HTML-Tabelle. Heute zeigt mein Parser alle Attribute an, unabhängig vom Comp-Wert, der zum Filtern verwendet wird. Jede Hilfe wird sehr geschätzt.

Mein Parser -

$.ajax({ 
    type: "GET", 
    url: 'sample.xml', 
    dataType: "xml", 
    success: function(xml) { 
     var compname = $(xml).find("prj\\:layout,layout").attr('comp'); 
     if(compname == "abcd") { 
      $(xml).find("prj\\:look, look").each(function() { 
      var $entry = $(this); 
      var pic = $entry.find("prj\\:lite,lite").each(function() { 
       var name = $(this).attr('name'); 
       $('<tr></tr>').html('<td>' + name + '</td>').appendTo('#listnames'); 
     }) 
     }) 
    } 
} 
    }) 

Antwort

1

Das Problem

Der Grund, warum Sie alle Attribute erhalten ist, dass Ihre var compname Erklärung nicht in einer Schleife ist. Jetzt fragen Sie: Aus allen Layouts, geben Sie mir ein Attribut namens comp. Da .attr() das Attribut des ersten Treffers zurückgibt, wird immer 'abcd' zurückgegeben. Wenn Sie nach compname = "abcd" suchen, ist es immer wahr.

Eine mögliche Lösung

Sie könnten die Layouts Schleife. Überprüfen Sie dann innerhalb dieser Schleife, was das Attribut compname für das aktuelle Layout ist. Dann überprüfe, ob das mit deinem Ziel-Compname übereinstimmt. Der Rest Ihres Codes funktioniert gut.

$.ajax({ 
    type: "GET", 
    url: 'sample.xml', 
    dataType: "xml", 
    success: function(xml) { 

    //First find the layouts 
    var layouts = $(xml).find("prj\\:layout,layout"); 

    //Loop through the layouts 
    layouts.each(function(){ 

     //Get this layout comp attribute 
     var compname = $(this).attr('comp'); 

     //check if the comp is our target comp 
     if(compname == "abcd") { 
     var $entry = $(this); 

     //Loop the 'lite' elements 
     var pic = $entry.find("prj\\:lite,lite").each(function() { 
      var name = $(this).attr('name'); 

      //Log for testing (remove in production) 
      console.log("found " + name); 

      //Your magic 
      $('<tr></tr>').html('<td>' + name + '</td>').appendTo('#listnames'); 

     }) 
     } 
    }) 

    } 
}) 

Eine effizientere Lösung

Sie auch Ihre Wähler in einer Weise strukturieren könnte, dass Sie die richtigen lite Elemente sofort

$.ajax({ 
    type: "GET", 
    url: 'sample.xml', 
    dataType: "xml", 
    success: function(xml) { 

    //Find the entries 
    var $entries = $(xml).find("prj\\:layout[comp='abcd'] prj\\:lite"); 

    //Loop the entries 
    $entries.each(function() { 
     var name = $(this).attr('name'); 

     //Log for testing (remove in production) 
     console.log("found " + name); 

     //Your magic 
     $('<tr></tr>').html('<td>' + name + '</td>').appendTo('#listnames'); 

    }) 

    } 
}) 
+0

Es zeigt bajaj, Honda, Yamaha und Honda bekommen während ich möchte, dass bajaj und honda nur angezeigt werden, wie ich Attribute unter comp = "abcd" brauche. – rnaikzz

+0

Alle Antworten zu diesem Thema? Ich bin immer noch ein wenig ahnungslos :( – rnaikzz

+0

Haben Sie Ihren Code wie ich vorgeschlagen aktualisiert? Wenn ja, zeigen Sie bitte aktualisierten Code –