Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    idn
    idn is offline
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting innerText bewteen node in XML document

    Hi,

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

    Below is a sample of the document:

    Code:
    <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

    Code:
    xmlDoc.getElementsByTagName("answer")[1].firstChild.nodeValue
    I only get the string

    Code:
    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!

  • #2
    Regular Coder fishluvr's Avatar
    Join Date
    Nov 2005
    Posts
    110
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Try changing your < > to &lt; and &gt; inside your "answer" tags in the xml file. They are creating additional child nodes.

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Or if for some reason you can't do that, you can try something liek this:

    Code:
    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
    Helping to build a bigger box. - Adam Matthews

  • #4
    idn
    idn is offline
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 thread, it would be awesome

    Thanks!

  • #5
    idn
    idn is offline
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Basscyst
    Or if for some reason you can't do that, you can try something liek this:

    Code:
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •