views:

51

answers:

3

Say I have xml like this:

<outer>
  <inner name="name0" type="type0" />
  <inner name="name1" type="type1" />
  <inner name="name2" type="type2" />
</outer>

I'm parsing the xml using jquery and I'm inside the each loop of the outer tag:

$(xml).find('outer').each(function() {
   //what goes here?
});

Say I know the values that can appear in the name id of <inner>. How do I, in the code above, get the appropriate type from the given <inner> tag.

Example:

I have the string var name = "name1" Inside the each loop, I need to pull type1 from the <inner> tag that has name="name1". Thanks

A: 

You can use an attribute-equals selector, like this:

var name="name1";
$(xml).find('outer').each(function() {
  var type = $(this).children("[name='" + name + "']").attr("type");
  //use type
});

You can give it a try here, if the XML is exactly what you posted and not a subset, just remove the .find('outer'), since the root element is already where you want to be.

Nick Craver
@downvoter - care to comment?
Nick Craver
@Nick - I did not downvote, but I suspect it has to do with this part - `$(xml)`. Everything else is rock solid.
Anurag
@Anurag - That part works, see the demo for yourself...this is how you *usually* deal with XML in jQuery, turning it into a document fragment first.
Nick Craver
@Nick - It's not about what works at the moment, but what is the right way to solve a problem. It is better that the OP understands the differences in parsing XML and HTML now, so that even if (s)he intends to use `$(xml)` as a quick-fix for XML parsing, they are fully aware of the consequences - that things can break. In that regards, a note of caution in your answer would be helpful to the OP and everyone else who comes across this problem looking for a solution.
Anurag
@Anurag - I can't say I agree, I don't follow "that things can break"...they work or they don't, nothing's breaking unless the browser or the input changes, in which case doesn't every piece of JavaScript you write fall under that? jQuery actively supports this and will make future changes necessary to support the API, so what exactly is going to break? It's not an undocumented or unsupported method we're calling...
Nick Craver
@Nick - "jQuery actively supports this" - no it doesn't. You're exploiting a side-effect of the `innerHTML` property which is nowhere documented for XML. The [jQuery() docs](http://api.jquery.com/jQuery/) clearly list all usages of this function, one of which accepts an HTML string, **not** XML. Just because you're calling a method that is part of the API does not make its usage appropriate or correct if you are trying to accomplish something totally different than what was intended for that method.
Anurag
@Anurag - Look at the documentation again, for `$.ajax()` where you'd be getting the XML: http://api.jquery.com/jQuery.ajax/ Under datatypes: "xml: Returns a XML document that can be processed via jQuery." It's getting the `responseXML` from the XmlHttpRequest, it's very intentionally and explicitly supported.
Nick Craver
@Nick - You are talking about something totally different. Now `$.ajax()` is not the same as `$()`, is it? Just to be very clear on this again as I said in the first comment itself - the problem is with parsing XML - specifically `$(xml)` in your code, not traversing XML which I called out as "rock solid".
Anurag
@Anurag - This would be taking place inside an `$.ajax()` success callback...how else are you getting the XML?
Nick Craver
@Nick: the `responseXML` property of the request object is an already parsed DOM, *not* a string, which is what @Anurag's [first link](http://api.jquery.com/jQuery/#jQuery2) explicitly states should not be an xml string.
Crescent Fresh
Hmm. I've assumed `xml` is a string. You've assumed it's an XML DOM Document. Looking at the question, it's not really clear if either of us are right! The word ‘parsing’ has been used, but perhaps in error.
bobince
Why are you making the assumption that it will come through ajax?
Anurag
@Crescent - I think there's some confusion here, I'm not advocating using a string for input, only in the situation you're getting XML, which is how I read the question.
Nick Craver
@Nick: fair enough. Your jsfiddle code clearly passes an xml string to `$`, which I agree with @Anurag could lead to confusion without a caveat that you're not suppose to do this.
Crescent Fresh
@all lol this is the moment where we all drop our assumptions and look towards the OP to find out how the XML is getting there in the first place :)
Anurag
@Crescent - Oh, that's just because I can't readily simulate an ajax request in fiddle to illustrate it well, sorry it was confusing, didn't mean it to be!
Nick Craver
A: 
var name = "name1";
$(xml).find('outer').each(function() {
   var type = $(this).children('[name=' + name + ']').attr('type');
});
Ender
I'm also curious about the downvote.
Ender
+3  A: 

I'm parsing the xml using jquery

Not really. $() does not include an XML parser; you are parsing it as HTML, using the browser's innerHTML parser. Because your input is not valid HTML, you might get any old strange DOM as the output, especially in IE, which doesn't like custom elements much.

It's annoyingly tricky to get an XML parser in a cross-browser way; it is much less well-supported than having XMLHttpRequest return an XML document. In many modern browsers you can ask for a new DOMParser, but for IE you have to create an MSXML2.DOMDocument ActiveXObject, and for a few older browser you have to document.implementation.createDocument (and even then the load method isn't standard or supported everywhere).

bobince
Eg http://stackoverflow.com/questions/2908899/jquery-wont-parse-xml-with-nodes-called-option/2910732#2910732 or http://stackoverflow.com/questions/3054411/how-do-i-parse-xml-with-jquery/3054523#3054523
Crescent Fresh