2

suppose I have two strings with XML inside. Something like this:

<playlist>
    <item id="1">
        ...
    </item>
</playlist>

and

<playlist>
    <item id="2">
        ...
    </item>
</playlist>

What I want to obtain is this:

<playlist>
    <item id="1">
        ...
    </item>
    <item id="2">
        ...
    </item>
</playlist>

Then I do this:

oldPlaylist = $.parseXML(string1);
newPlaylist = $.parseXML(string2);
$(oldPlaylist).find('playlist').each(function(index,playlist) {
    $(newPlaylist).find('item').each(function(index2,item) {
        $(playlist).add(item);
    });
});
answer = (new XMLSerializer()).serializeToString(oldPlaylist);

But it doesn't work. Answer is the same that string1. I can say that with the example supplied, this enters to the first each one time, and to the second each one time too (then seems that it reads correctly both XML).

What do you think? Can you help me? Thank you!

4
  • Are you using jQuery 1.6? (I see the find method which operates on elements is only added in 1.6) Commented May 17, 2011 at 14:50
  • I'm using 1.5.1 but the way I'm using find (with a selector) is added since 1.0 (api.jquery.com/find) Commented May 17, 2011 at 14:56
  • Yes, you're right... my bad... I thought that 'playlist' was seen as an element... but apparently a string is always a selector... Commented May 17, 2011 at 14:59
  • Looks to me like add() is the wrong thing to use. It merely adds the item to the set of matched elements... it does not insert a new child element into a DOM tree. Commented May 17, 2011 at 16:18

2 Answers 2

4

According to the documentation, the add method constructs a new jQuery object and returns it. It doesn't actually change the object on which you call the method.

So it's normal that the answer is the same string as string1.

You could do something along these lines:

var oldPlaylist = $.parseXML(string1);
var newPlaylist = $.parseXML(string2);
$(newPlaylist).find('item').each(function(index,item) {
    oldPlaylist = oldPlaylist.add(item);
});
answer = (new XMLSerializer()).serializeToString(oldPlaylist);
Sign up to request clarification or add additional context in comments.

4 Comments

I can't test it with all the project until tomorrow but I think that this will be the thing, or at least it solves part of the mistake. Thank you!
If this works at all, it should give you a set that is a union of the old playlist and the new item... the new item is not inserted as a child of the old playlist.
@LarsH: Yep, could be... that's why I said "along these lines" ;-) My point is that you have to assign the value returned by add somewhere.
You're right that if you use add(), then in order for it to have any effect, you have to do something with the return value. But since add() cannot do what the OP wants no matter what is done with the return value, that point really doesn't help the OP accomplish his goal.
2

Looks to me like add() is the wrong thing to use. It merely adds the item to the set of matched elements... it does not insert a new child element into a tree.

Instead, you want append(). And append() actually modifies the object it's operating on, so you don't need to assign the result to oldPlaylist.

So I would try something like

var oldPlaylist = $.parseXML(string1);
var newPlaylist = $.parseXML(string2);
$(oldPlaylist).find('playlist').each(function(index, playlist) {
    $(newPlaylist).find('item').each(function(index2, item) {
        $(playlist).append(item);
    });
});
answer = (new XMLSerializer()).serializeToString(oldPlaylist);

I tested this and it gives the desired answer:

<playlist>
  <item id="1"> ... </item>
  <item id="2"> ... </item>
</playlist>

(Indentation added for clarity.)

1 Comment

+1 This might indeed be a better solution for the problem at hand. My answer merely points out the flaw in the OPs current "design".

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.