jsdom xml, update element value

230 views
Skip to first unread message

Joe

unread,
Feb 22, 2013, 4:50:50 PM2/22/13
to js...@googlegroups.com
Hello,

Im not sure whether this is the correct place to ask the question. If not, please excuse me. Following is my question:

Im trying to using jsdom to parse an xml, and update certain values and recreate the xml content. Is the below code correct. The new value "NewPassword" is not changed. And the window.document prints the complete document. How to get only the xml file. Thanks


var xml = '<?xml version="1.0" encoding="UTF-8"?>\
<login>\
       <username>tests string</username>\
       <password>test string</password>\
   </login>';

var jsdom = require("jsdom").jsdom;
jsdom.env({
    html: xml,
    done: function(errors, window) {
        x = window.$("password").text();
        console.log(x)
        x.nodeValue="NewPassword";
        x = window.$("password").text();
        console.log(window.document)
    }
});

Thanks,
Joe

Elijah Insua

unread,
Feb 22, 2013, 5:09:29 PM2/22/13
to js...@googlegroups.com
$('el').text() returns a string, not a DOM node.

If you are already using jquery, why not use $(el).text('new value') instead? Also, I'm not convinced that .nodeValue works that way.


"In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment), this returns null."

which can be seen by doing:

var x = window.$('password');
x.nodeValue = "something";
console.log(x.nodeValue); // null

Here's your code, in a functional state: https://gist.github.com/tmpvar/5016926

Please add an issue and appropriate specification link if this is not the correct behavior.

-- Elijah


--
You received this message because you are subscribed to the Google Groups "jsdom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsdom+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Joe

unread,
Feb 22, 2013, 7:24:11 PM2/22/13
to js...@googlegroups.com
Great. Thanks Elijah, this works. One more question is, I want to get the modified XML content 

when I try 

console.log(window.document.innerHTML);

The output, I get is

<html><body><login>       <username>tests string</username>       <password>NewPassword</password>   </login></body><script class="jsdom" src="http://code.jquery.com/jquery.js"></script></html>


Is there a way to get the original xml content, but with the modified new value

<?xml version="1.0" encoding="UTF-8"?><login>       <username>tests string</username>       <password>NewPassword</password>   </login>

Thanks,
Joe.

Joe

unread,
Feb 22, 2013, 7:50:36 PM2/22/13
to js...@googlegroups.com
Got it, by using

console.log(window.document.body.innerHTML);

Thanks,
Joe.
Reply all
Reply to author
Forward
0 new messages