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

时间:2021-10-19 01:43:28

I have this XML file here.

我这里有这个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>

And then I will load them and store these words in a array called word once the page is done loading

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

$(document).ready(function() {

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

})

})

and then when I access each contents of alert(word[0]) it shows me this result

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

RIP
PIE  
PIER
RIPE
SPIRE 
SPIDER

So I am assuming that word[0] is something like this, word[0] = "RIP PIE PIER RIPE SPIRE SPIDER "

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

but when I do this "

但是当我这样做时“

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

it is not giving me the word "RIP" any idea why is this happening? I want to disect all of the words in words[0](that came from an xml) and then split those words and store those words in an array but it seems to be not working any idea why?

它没有给我“RIP”这个词任何想法为什么会发生这种情况?我想要删除单词[0]中的所有单词(来自xml),然后拆分这些单词并将这些单词存储在一个数组中,但似乎无法理解为什么?

1 个解决方案

#1


1  

May be something like this

可能是这样的

$.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]);​

#1


1  

May be something like this

可能是这样的

$.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]);​