简体   繁体   English

用JS解析一些XML:

[英]parsing some XML with JS:

I have a XML file that is used by Flash as string resource 我有一个Flash用作字符串资源的XML文件

I would like my JS to use that XML as well: is there an easy way to read and parse external XML file in JS ? 我希望我的JS也使用该XML:是否有一种简单的方法来读取和解析JS中的外部XML文件?

like resources.xml that contains: 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>

.... ....

If you want to get every node and the text it contains, try this: 如果要获取每个节点及其包含的文本,请尝试以下操作:

        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);
        });

The output will be: 输出将是:

elem is
sizeup

text is
Size up

elem is  
sizedown

text is  
Size Down 

If you are looking for the contents of a specific tag, you could do: 如果要查找特定标签的内容,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM