8

I am trying to parse an xml formatted string, remove one of its elements, and write it back to a string. All with standard jQuery functions. It's the last step that I can't seem to figure out.

var $xml = $(somexmlstring);
var element = $xml.find('name:contains("'+somevalue+'")');
element.remove();
var newxmlstring = $xml.dunno(); 

What function can I use to convert the $xml DOM back to a string?

4 Answers 4

6

To help you fix your code in the jsfiddle:

var data = '<value><url>foo</url><name>bar</name></value><value><url>foo</url><name>bar</name></value>'
var xml = $.parseXML('<root>' + data + '</root>');
var $xml = $(xml);
console.log('xml', $xml.find('root').html());

The fix is in the last line: `$xml.find('root').html()

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

4 Comments

This does not work in Internet Explorer though. The browser says that innerHTML does not have a .replace() property...
@AlexisWilke: that is probably just an issue with jquery and IE and has nothing to do with the code from the answer
I think there is a discrepancy between XML and HTML and the html() function expects HTML only... When I tried this I had problems with a true XML document (not just HTML.) Possibly because one of the tags was named <title> and it was not inside a tag named <head>.
@AlexisWilke: I experienced quite some frustration with this difference between XML and HTML.... Indeed using tags that are for instance not supposed to have children in HTML but had children in XML (and why not) did go completely apewire on me in jQuery... Must admit that we did not use $.parseXML in that time.....
3

You can use .html() if you wrap the jQuery object with a parent element first and then call .html() on the parent element.

var $xml = $(somexmlstring);
$xml.find('name:contains("' + somevalue + '")').remove();
var newxmlstring = $('<x></x>').append($xml).html(); 

This works even if the original XML string does not have a single root element. It does not work, however, if the original XML string contains an XML declaration (like <?xml version="1.0" encoding="UTF-8"?>).

jsfiddle


If the original XML string is a valid XML document, optionally containing an XML declaration at the beginning, you may want to create the jQuery object like this:

var $xml = $($.parseXML(somexmlstring).documentElement);
$xml.find('name:contains("' + somevalue + '")').remove();
var newxmlstring = $('<x></x>').append($xml).html(); 

jsfiddle

3 Comments

tried your jsfiddle, but this does only return the first node, not the whole string that goes in: data != $xml.wrap('<x></x>').parent().html()
$.parseXML(data); will fail because data is not proper xml, it has more than one node, that's why you first wrap the string and parse the result to XML: var xml = $.parseXML('<root>' + data + '</root>');
@RemyNL - Thanks. I must have missed that the fiddle did not work. The issue was with the use of .wrap(). It wraps all top level nodes. The fix is to use .append() instead. You are also correct about $.parseXML() requiring the XML string to have a single root element.
2

You dont need jquery to this, use XMLSerializer

new XMLSerializer().serializeToString(xmlobj.documentElement);

Comments

0

As long as your XML document does not contain some extra fancy namespaces, then it's simple:

var newxmlstring = $xml.html(); 

2 Comments

That's one of the first things I've tried, but I can't get it to work. jsfiddle.net/0x80/GwpQ5/3
I can parse the xml string, search in the nodes etc, but whenever I call the html() function it seems to return undefined. I can call text() and it will return the content of the xml nodes without the tags.

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.