用JS解析一些XML:

时间:2013-12-29 19:54:12

标签: javascript xml xml-parsing

我有一个Flash文件,它被Flash用作字符串资源

我希望我的JS也能使用那个XML:有一种简单的方法可以在JS中读取和解析外部XML文件吗?

类似 resources.xml ,其中包含:

<resources locale="en">
<sizeUp>Size up</sizeUp>
<sizeDown>Size Down</sizeDown>
<clearSearch>Clear search</clearSearch>
<wouldYouLikeToReport>Would you like to report this user for abuse ?</wouldYouLikeToReport>
<yes>Yes</yes>

...

1 个答案:

答案 0 :(得分:0)

如果您想获取每个节点及其包含的文本,请尝试以下操作:

        var xml;
        xml = '<resources locale="en">';
        xml += '<sizeUp>Size up</sizeUp>';
        xml += '<sizeDown>Size Down</sizeDown>';



        $(xml).children().each(function(index, elem) { 
            var $element = $(elem);         
            var text =  $element.text();

            console.log('elem is ');
            console.log(elem);
            console.log('text is ');
            console.log(text);
        });

输出将是:

elem is
sizeup

text is
Size up

elem is  
sizedown

text is  
Size Down 

如果您要查找特定标签的内容,可以执行以下操作:

console.log('sizeUp is ');
console.log($(xml).find('sizeUp').text());