/    Sign up×
Community /Pin to ProfileBookmark

need to insert part of XML into DOM

Hi All

I would like to insert a part (an element with all its children) from XML file into the dom. So far I’m able to load the XML via AJAX and extract the node of interest

[code]
var node_tags = ajaxresp.responseXML.getElementsByTagName(‘*’) ;
for(var i = 0; i < node_tags.length; i++) {
if ( node_tags[i].getAttribute(‘id’) == id ) {
document.getElementById(‘container’).appendChild(node_tags[i]) ; // FF. Doesn’t seem to do much in IE
document.getElementById(‘container’).innerHTML = node_tags[i].xml ; // IE. In FF node_tags[i].xml is ‘undefined’
}
}
[/code]

Is there a way to do this which is the same for all browsers ? (I prefer a solution using ‘innerHTML’!)

thnx a lot
LuCa

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 30.2009 — Might just have to do it this way:
[CODE] if ( node_tags[i].getAttribute('id') == id ) {
if(node_tags[i].xml)
document.getElementById('container').innerHTML = node_tags[i].xml;
else
document.getElementById('container').appendChild(node_tags[i]);
}[/CODE]
Copy linkTweet thisAlerts:
@jeanlucaauthorJan 30.2009 — thnx!

But what if you have to replace the content once in a while with something else. With your solution I guess you have to remove the added children before you can insert the new content! Thats why I prefer to use [b]innerHTML[/b] in all browsers!
Copy linkTweet thisAlerts:
@MrNobodyJan 30.2009 — Yes, you'll have to remove any unwanted children (how sad) before adding more to the parent. ?
Copy linkTweet thisAlerts:
@jeanlucaauthorJan 30.2009 — what about this
<i>
</i>if ( node_tags[i].getAttribute('id') == id ) {
try { // FF, Chrome, Safari and Opera
return (new XMLSerializer()).serializeToString(node_tags[i]);
} catch(e) { // IE
return node_tags[i].xml ;
}
}

this way, you don't need to remove any children before inserting new content ?
×

Success!

Help @jeanluca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...