简体   繁体   English

访问xml文件节点?

[英]access to a xml file nodes?

i have a xml file like this 我有一个这样的xml文件

<root>
    <section>
        <state>state one</state>
        <ct>
            <city>first city</city>
            <city>second city</city>
            <city>third city</city>
        </ct>
    </section>
</root>

and i want to access to city names in jquery...i have tried s.th like this: 而且我想访问jquery中的城市名称...我已经尝试过s.th像这样:

var x;
var ct = "";
for(i=1 ; 1=3 ; i++){
x = xmlDoc.getElementsByTagName('ct')[0].childNodes[i].childNodes[0].nodeValue;
ct += x;
}
$('div').html(ct);

BUT, i only have the first city.... and i couldnt access to second and third.. a code like this: 但是,我只有第一个城市....而我无法访问第二和第三个..这样的代码:

x = xmlDoc.getElementsByTagName('ct')[0].childNodes[2].childNodes[0].nodeValue;

give me nothing!!!!! 什么都不给我!

Since you mentioned jQuery, you can use $.parseXML to parse the XML to an XML Document, $() to get a jQuery wrapper for that document, find to find the city nodes, and then do what you like with them (such as using map to get their text): 自从您提到jQuery以来,您可以使用$.parseXML将XML解析为XML文档,使用$()获取该文档的jQuery包装器, find city节点,然后对它们进行所需的处理(例如使用map获取其文字):

 var doc = $.parseXML( '<root>' + '<section>' + '<state>state one</state>' + '<ct>' + '<city>first city</city>' + '<city>second city</city>' + '<city>third city</city>' + '</ct>' + '</section>' + '</root>' ); var cities = $(doc).find("city"); var cityNames = cities.map(function() { return $(this).text(); }).get(); console.log(cityNames); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

( get after map converts the jQuery wrapper that map returns into a plain array.) getmap转换jQuery的包装, map返回到普通数组)。

The selector we used in find can be more specific. 我们在find使用的选择器可以更具体。 For instance, you specifically looked for city elements within a ct container; 例如,您专门在ct容器中寻找city元素; if that's important, our find selector should be ct > city rather than just city : 如果那很重要,我们的find选择器应该是ct > city而不是city

 var doc = $.parseXML( '<root>' + '<section>' + '<state>state one</state>' + '<ct>' + '<city>first city</city>' + '<city>second city</city>' + '<city>third city</city>' + '</ct>' + '</section>' + '</root>' ); var cities = $(doc).find("ct > city"); var cityNames = cities.map(function() { return $(this).text(); }).get(); console.log(cityNames); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

How many childNodes do you see inside the <ct> element? 您在<ct>元素内看到多少个childNodes Most probably 3. Actually, there are 7 childNodes in there. 最可能是3。实际上,那里有7个childNodes Three of them are your <city> elements and additional four are text nodes made of whitespaces (spaces, line breaks, carriage returns) between xml tags. 其中三个是您的<city>元素,另外四个是由xml标记之间的空格(空格,换行符,回车符)组成的文本节点。

You should use functions that return array of elements rather than array of nodes (eg. getElementsByTagName). 您应该使用返回元素数组而不是节点数组的函数(例如,getElementsByTagName)。

var cities = xmlDoc.getElementsByTagName('ct')[0].getElementsByTagName('city');

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

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