??yy 2015-11-11 03:15 采纳率: 0%
浏览 42

解析XML中的标签

Need to parse the XML and fetch specific tag's value

We need to display just the tags where correct words are placed - "sports" and "secure" which are appearing in tag <str name="word">WORD</str> in below XML

XML

<response>
    <lst name = "responseHeader">
        <int name status>0</int>
        <int name QTime>3</int>
    </lst>
    <result name="response",numFound="0",start="0"/>
    <lst name="spellcheck">
      <lst name="suggestions">
        <lst name="sporsts">
          <int name="numFound">1</int>
          <int name="startOffset">0</int>
          <int name="endOffset">5</int>
          <int name="origFreq">0</int>
          <arr name="suggestion">
            <lst>
              <str name="word">sports</str>
              <int name="freq">1</int>
            </lst>
          </arr>
        </lst>
        <lst name="secuer">
          <int name="numFound">1</int>
          <int name="startOffset">6</int>
          <int name="endOffset">17</int>
          <int name="origFreq">0</int>
          <arr name="suggestion">
            <lst>
              <str name="word">secure</str>
              <int name="freq">1</int>
            </lst>
          </arr>
        </lst>
      </lst>
      <bool name="correctlySpelled">false</bool>
    </lst>
</response>

Currently, using ajax to fetch the data by calling ajax as mentioned here : In the ajax call :

$.ajax({
   type: "GET",
   url: solrurl,
   dataType: "xml",
   cache: false,
   success: function(data) {
        console.log(data);
         },      
     error : function(data)
     { 
          alert(Contact the Solr Admin);
     }
});

Tried using response.spellcheck.suggestions[0], but its throwing error - tried by putting other parameters as well, but it didn't work!

Could anyone please suggest or advise on getting just the words sports and secure in an array.

  • 写回答

1条回答 默认 最新

  • weixin_33714884 2015-11-11 07:31
    关注

    I have updated your code and have checked it and it works fine.

    JQUERY

    $(document).ready(function () {
        var Values = new Array();
        $.get("chckvar.php",{},function(xml){
            $("str",xml).each(function(i){
                Values.push($(this).text());
            });
            console.log(Values)
        });
    });
    

    In the browsers console window you would be able to see the output as

    ["sports", "secure"]
    

    I hope this is what you were looking for.

    评论

报告相同问题?