

Obtain an XML element by index rather than Tag
source link: https://www.codesd.com/item/obtain-an-xml-element-by-index-rather-than-tag.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Obtain an XML element by index rather than Tag
I'm creating a video library and have an XML document split up by video category such as:
<video>
<comedy>
<url>bla</url>
<title>blabla</title>
</comedy>
<action>
<url>bla</url>
<title>blabla</title>
</action>
</video>
And so on. I use an XMLHttpRequest to getElementsByTagName() for the genre I want and it is working fine.
My question is: I want to create a 'Most Recent' category, that would just pick the first 16 (or however many) off the top of the XML file, regardless of category. Is there a way to accomplish this?
Iterate over the childNodes of the root-element, and check if the node is a element-node(childNodes will also return textNodes for the whitespaces)
var doc=xml,//the xml-document
childs=doc.documentElement.childNodes,
i=-1,
j=16;//how many nodes you like to get
while(j && childs[++i])
{
if(childs[i].nodeType===1)//check if we got a element-node
{
j--;
//do something with the element
console.log(childs[i].tagName);
}
}
In some browsers there may also be a children-property which only returns children that are element-nodes, but this is no standard so I wouldn't suggest to use it.
When you use a library like jQuery it would be much easier, to have the same result you only need:
$('>*:lt(16)',xml.documentElement)
.each(function(i,o){console.log(o.tagName);});
Recommend
-
12
Dispatch rather than check types written by Eric J. Ma on 2021-01-24 | tags:
-
6
How to lead a team by principles rather than processGuiding principles of high-performing (UX) teams.
-
11
Golang: why sort takes value rather than pointer For golang, every time a variable is passed as parameter, a new copy of the variable is created and passed to called function or method. The copy is allocated at a different memory ad...
-
10
Use poisson rather than regress; tell a friend Do you ever fit regressions of the form ln(yj) = b0 + b1x1j + b2x2j + … + bkxkj + εj
-
12
Obtain an inverted slice of a table that includes the base element advertisements I'm using numpy arrays. I'm trying to calculate an array tha...
-
10
How to encourage DBAs to embrace DevOps, rather than fear change How do we help Database A...
-
12
Caliburn.micro bind element visibility to viewmodel's function rather than a property advertisements first of all - I'm sor...
-
13
How to Print the Index or Obtain the Element of an Item in an Array List advertisements So I'm making a program that makes...
-
4
Obtain a list of files from the Apache index page using Ruby / Rails advertisements I am attempting to create a radar anima...
-
2
本文是 Element 的组件源码学习系列。 项目源码:ElemeFE/element | GitHub,Tag:v2.13.0 Tag 组件使用文档:T...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK