从XML文件中排除一组单词,并将它们存储在Javascript中的数组中

Disecting a group of words from an XML file and storing them in an array in Javascript

本文关键字:存储 数组 Javascript 单词 排除 文件 XML 一组      更新时间:2023-09-26

这里有这个XML文件。

<?xml version="1.0" encoding="UTF-8"?> 
<xml> 
<word> 
  <threeletter>RIP</threeletter>
  <threeletter>PIE</threeletter>  
  <fourletter>PIER</fourletter>
  <fourletter>RIPE</fourletter>
  <fiveletter>SPIRE</fiveletter> 
  <sixletter>SPIDER</sixletter> 
 </word>
 <word> 
  <threeletter>SUE</threeletter> 
  <threeletter>USE</threeletter> 
  <fourletter>EMUS</fourletter>
  <fourletter>MUSE</fourletter>
  <fiveletter>SERUM</fiveletter> 
  <sixletter>RESUME</sixletter> 
 </word>
</xml>

然后我会加载它们,并在页面加载后将这些单词存储在一个名为word的数组中

$(document).ready(function() {
    $.ajax
    ({ 
        url: "dictionary.xml", 
        success: function( xml )
        { 
            $(xml).find("word").each(function()
            { 
            words.push($(this).text());
            }); 
        }       
    });

})

然后当我访问alert(word[0])的每个内容时,它会向我显示这个结果

RIP
PIE  
PIER
RIPE
SPIRE 
SPIDER

所以我假设单词[0]是这样的,word[0] = "RIP PIE PIER RIPE SPIRE SPIDER "

但是当我做这个"

var x = word[0].split(" ");
                    alert(x[0]);

它没有给我"RIP"这个词知道为什么会发生这种事吗?我想去掉words[0]中的所有单词(来自xml),然后拆分这些单词并将这些单词存储在一个数组中,但似乎不起作用——不知道为什么?

可能类似于此

$.ajax({
    url: 'dictionary.xml',
    async: false,
    success: function(xml) {
        $(xml).find("word").each(function(index) {
            words[index] = [];
            $(this).children().each(function() {
                words[index].push($(this).text());
            });
        });
    },
    dataType: 'XML'
});
console.log(words[0]);
console.log(words[1]);​