使用 Jquery 查找 XML 元素值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12256547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:29:07  来源:igfitidea点击:

Finding an XML element value using Jquery

jqueryxmlajax

提问by Lamis

I have an XML file that I need to find the item of specific PK using Jquery and Ajax so far I get to know the object but I have two questions :

我有一个 XML 文件,我需要使用 Jquery 和 Ajax 找到特定 PK 的项目,到目前为止我了解了该对象,但我有两个问题:

  1. Is there a better idea than doing a loop to find the pk value?
  2. the XML is large and I need to know if there is a better way to query it than loading it into memory, does XSLT help better? or is there anyway to do it better than jquery?
  1. 有没有比循环查找 pk 值更好的主意?
  2. XML 很大,我需要知道是否有比将其加载到内存中更好的查询方式,XSLT 是否有更好的帮助?或者无论如何比jquery做得更好?

Here is my code

这是我的代码

$.ajax({
    url: 'xml/products.xml',
    dataType: 'html',
    success: function(xml) {
        $(xml).find('pk').each(function() {
            if ($(this).text() == "1")
            //do something
        });
    }
});

And here is my xml

这是我的 xml

<products>
<item>
    <pk>1</pk>
    <name>test</name>
</item>
<item>
    <pk>2</pk>
    <name>test2</name>
</item>
<item>
    <pk>3</pk>
    <name>test3</name>
</item>
<item>
    <pk>4</pk>
    <name>test4</name>
</item>
</products>

回答by gaurang171

First, you have to write correct XML string, like you have to complete/ending same tag which has been started last one. on above sample code, you have done mistake with closing . it is wrong xml syntax. please make correction as below: 1 test

首先,您必须编写正确的 XML 字符串,就像您必须完成/结束最后一个开始的相同标签一样。在上面的示例代码中,您在关闭时犯了错误。这是错误的 xml 语法。请按以下方式进行更正: 1 次测试

Here i have made on sample bins for parsing XML data or tags, Instead of Ajax i have just parse xml data on button click event because on bins Ajax call is not possible to call external file.

在这里,我制作了用于解析 XML 数据或标签的示例 bin,而不是 Ajax,我只是在按钮单击事件上解析 xml 数据,因为在 bin 上 Ajax 调用无法调用外部文件。

Here is Demo:http://codebins.com/bin/4ldqp7u

这是演示:http : //codebins.com/bin/4ldqp7u

HTML

HTML

<div>
  <input type="button" id="btnxml" value="Get XML Data" />
  <input type="button" id="btnreset" value="Reset" style="display:inline"/>
  <div id="result">
  </div>
</div>
<div id="xmldata">
  <products>
    <item>
      <pk>
        1
      </pk>
      <name>
        test
      </name>
    </item>
    <item>
      <pk>
        2
      </pk>
      <name>
        test2
      </name>
    </item>
    <item>
      <pk>
        3
      </pk>
      <name>
        test3
      </name>
    </item>
    <item>
      <pk>
        4
      </pk>
      <name>
        test4
      </name>
    </item>
  </products>
</div>

JQuery:

查询:

$(function() {
    $("#btnxml").click(function() {
        var xml = "<rss version='2.0'>";
        xml += $("#xmldata").html();
        xml += "</rss>";
        var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);

        var result = "";
        if ($xml.find("item").length > 0) {

            result = "<table class='items'>";
            result += "<tr><th>PK</th><th>Name</th></tr>";

            $xml.find("item").each(function() {
                result += "<tr>";
                result += "<td>" + $(this).find("pk").text() + "</td>";
                result += "<td>" + $(this).find("name").text() + "</td>";
                result += "</tr>";
            });

            result += "</table>";
            $("#result").html(result);
        }


    });

    //Reset Result 
    $("#btnreset").click(function() {
        $("#result").html("");
    });

});

CSS:

CSS:

#xmldata{
  display:none;
}
table.items{
  margin-top:5px;
  border:1px solid #6655a8;
  background:#55a5d9;
  width:20%;
}
table.items th{
  border-bottom:1px solid #6655a8;
}
table.items td{
  text-align:center;
}
input[type=button]{
  border:1px solid #a588d9;
  background:#b788d9;
}

Demo:http://codebins.com/bin/4ldqp7u

演示:http : //codebins.com/bin/4ldqp7u

回答by bhamlin

At the very least, you can use a more specific query than just "pk". In this example, $(xml).find("products item pk")should be faster.

至少,您可以使用比“pk”更具体的查询。在这个例子中,$(xml).find("products item pk")应该更快。