0

this is my first question on stack overflow, I hope you guys can help me.

here is my xml

<?xml version="1.0"?>
<RecentPhotoUploads>
<PHOTO><SOURCE>6171.jpg</SOURCE></PHOTO>
<PHOTO><SOURCE>6173.jpg</SOURCE></PHOTO>
<PHOTO><SOURCE>6895.jpg</SOURCE></PHOTO>
<PHOTO><SOURCE>Tulips.jpg</SOURCE></PHOTO>
</RecentPhotoUploads>

here is my javascript code

    var xml=loadXMLDoc("../upload.xml");
    var source="/RecentPhotoUploads/PHOTO/SOURCE"

    var nodes=xml.evaluate(source, xml, null, XPathResult.ANY_TYPE, null);
    var result=nodes.iterateNext();


                while (result)
                {
                document.write("<h2>" + result.childNodes[0].nodeValue + "</h2>");
                document.write("<a href='../pics/" + result.childNodes[0].nodeValue + "</a>");
                result=nodes.iterateNext();
                }

How would I loop through this xpathresult backwards?

1
  • Loop it forwards, push each result onto an array, pop the array in a loop to get the result backwards. Commented Sep 6, 2012 at 23:42

3 Answers 3

2

Why you would navigate backwards? You can just print backwards ...

var xml=loadXMLDoc("../upload.xml");
var source="/RecentPhotoUploads/PHOTO/SOURCE"

var nodes=xml.evaluate(source, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();

var buffer = "";
while (result) {
    buffer = "<h2>" + result.childNodes[0].nodeValue + "</h2>"
           + "<a href='../pics/" + result.childNodes[0].nodeValue + "'>"
           + result.childNodes[0].nodeValue + "</a>"
           + buffer;
    result=nodes.iterateNext();
}
document.write(buffer);

without resorting to documentFragment and similar modern tools ;)

Sign up to request clarification or add additional context in comments.

4 Comments

Great idea! You might want to use a StringBuilder object with your methodology to do the pre-pending, as described here: stackoverflow.com/questions/738950/…
@M3NTA7: I don't think javascript has a StringBuilder object
Javascript's way is to simply unshift() into an array and then join() it.
Sorry about the StringBuilder comment. I totally missed that it was javascript.
0

Here's an article that shows one way of sorting and iterating:

http://blogs.msdn.com/b/kaevans/archive/2006/04/17/577456.aspx

Comments

0

First change the settings for your XPathResult. You want an ordered snapshot type. iterateNext() won't work because it doesn't have numeric keys.

var result = document.evaluate(source, xml, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

Then loop backwards like so.

for (var i = result.snapshotLength - 1; i >= 0; i--){
    document.write("<h2>" + result.snapshotItem(i).textContent + "</h2>");
    document.write("<a href='../pics/" + result.snapshotItem(i).textContent + "'>" 
    + result.snapshotItem(i).textContent + "</a>");

}

PS: you're missing a '>' in your code for the <a> tag. Not sure what output you're aiming for so I took a guess.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.