...

View Full Version : Getting innerText bewteen node in XML document



idn
12-15-2005, 07:47 PM
Hi,

I am having problems getting the inner value of a node in an xmldocument.

Below is a sample of the document:



<help>
<topic id="1" admin="false">
<question>How do I change the relationship between this portal and other portals in the related portals section?</question>
<answer>The portals can be arranged in a hierarchical structure to allow users to better navigate between the different portals, if you want a portal to appear underneath another portal, or want another portal to be listed under this portal, please contact the <a href="mailto:jbolt@foo.com">admin</a> stating which portal you want to move and where you want to move it to.</answer>
</topic>
<topic id="2" admin="false">

<question>How do I retire a portal?</question>
<answer>Please contact the <a href="mailto:jbolt@foo.com">admin</a> stating which portal needs to be retired. By retiring the portal, the relevant modifier will no longer be visible in the document id tool; however the portal and all its documentation will still be accessible by viewing the <a href="http://dmportls-wxp/dmportls/?modifier=RETIRED">RETIRED</a> portal.</answer>
</topic>
</help>


When I use the code


xmlDoc.getElementsByTagName("answer")[1].firstChild.nodeValue

I only get the string


Please contact the

It doesnt retrieve the <a> tag or anything after it, is there something similar to teh innerHTML funciton you can use on HTML elements to get the complete contents of a tag?

Thanks!

fishluvr
12-15-2005, 08:31 PM
Try changing your < > to &lt; and &gt; inside your "answer" tags in the xml file. They are creating additional child nodes.

Basscyst
12-15-2005, 08:40 PM
Or if for some reason you can't do that, you can try something liek this:



function writeData(req)
{
doc=req.documentElement;
var ans=doc.getElementsByTagName('answer')
var len=ans.length;
var obj=document.getElementById('data_spot')
for(var i=0;i<len;i++)
{
var kids=ans[i].childNodes;
for(var j=0;j<kids.length;j++)
{
if(!kids[j].tagName)
{
obj.innerHTML+=kids[j].nodeValue;
}
else if(kids[j].tagName&&kids[j].tagName.toLowerCase()=='a')
{
var a=document.createElement('a');
a.setAttribute('href',kids[j].getAttribute('href'));
a.innerHTML=kids[j].text;
obj.appendChild(a);
}
}
obj.appendChild(document.createElement('hr'));
}
}


Basscyst

idn
12-15-2005, 08:45 PM
Great, it works! Thanks alot, it's a shame I have to do it as it makes the XML a little less readable, never mind though.

If you have any ideas for this (http://www.codingforums.com/showthread.php?p=386463&posted=1#post386463)thread, it would be awesome

Thanks!

idn
12-15-2005, 08:47 PM
Or if for some reason you can't do that, you can try something liek this:



function writeData(req)
{
doc=req.documentElement;
var ans=doc.getElementsByTagName('answer')
var len=ans.length;
var obj=document.getElementById('data_spot')
for(var i=0;i<len;i++)
{
var kids=ans[i].childNodes;
for(var j=0;j<kids.length;j++)
{
if(!kids[j].tagName)
{
obj.innerHTML+=kids[j].nodeValue;
}
else if(kids[j].tagName&&kids[j].tagName.toLowerCase()=='a')
{
var a=document.createElement('a');
a.setAttribute('href',kids[j].getAttribute('href'));
a.innerHTML=kids[j].text;
obj.appendChild(a);
}
}
obj.appendChild(document.createElement('hr'));
}
}



Basscyst



Thanks, I'll check out your code and get back to you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum