Why don't I get the node value of the code node?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    #1

    Why don't I get the node value of the code node?

    I just happened to learn abt xml DOM.....I tried this script and run it bt didnt get the desired result..I expected to get the value I attached to my code node bt what I gt ws null which means that I went wrong somewhere in my script....


    Code:
    <html>
    	<head>
    		<script type='text/javascript'>
    			xml_modules="<modules><module><code></code></module></modules>";
    			var xml_modules;
    			
    			function addxml(url)
    			
    				{
    					var xmlDoc;
    					try{
    						xmlDoc=new ActiveXObject('Microsoft.XMLDOM');
    						xmlDoc.async='false';
    						xmlDoc.loadXML(url);
    						return xmlDoc;
    					}
    					catch(e){
    						
    						try {
    							parser=new DOMParser();
    							xmlDoc=parser.parseFromString(url,'text/xml');
    							xmlDoc.encoding='UTF-8';
    							return xmlDoc;
    							}catch(e) {alert(e.message);}
    						}
    						return (null);
    					
    				} 
    				var xml_Doc_modules=addxml(xml_modules);
    				
    				function textxml()
    					{
    						
    						root=xml_Doc_modules.childNodes[0];
    						a=xml_Doc_modules.createTextNode('node1');
    							
    						root.childNodes[0].childNodes[0].appendChild(a)	;
    						
    						alert(root.childNodes[0].childNodes[0].nodeValue);
    						
    						
    						
    					} 
    				
    		
    		</script>
    	</head>
    <body>
    <button onclick='textxml()'>ProcessXml</button>
    
    </body>

    This is the script....!!!!
    I went wrong somewhere in my codes bt cant guess where....
    Can u tell me where I went wrong and how can I attach a value to a node ??


    Thanks in adv

    With regards
    phpuser123
    Last edited by Dormilich; Apr 17 '10, 06:32 PM. Reason: Please use [code] tags when posting code
  • phpuser123
    New Member
    • Dec 2009
    • 108

    #2
    Why the value of my xml node is not diaplayed?

    I jst happened to learn abt xml DOm...For trial purposes i implemented this script
    ..
    Code:
    <html>
    	<head>
    		<script type='text/javascript'>
    			var xml_modules;
    			xml_modules="<modules><module><code></code><tbl></tbl></module></modules>";
    			var xml_Doc_modules=loadString(xml_modules);
    			function loadString(txt)
    					{
    						var xml_Doc;
    						try{
    							xmlDoc=new ActiveXObject("Microsoft.XMLDOM")	;
    							xml_Doc.async="false";	
    							xml_Doc.loadXML(txt);
    							return xml_Doc;
    							}
    						catch(e)
    							{
    								try{
    									parser=new DOMParser();
    									xml_Doc=parser.parseFromString(txt,"text/xml");	
    									xml_Doc.encoding='UTF-8';alert('sad');
    									return xml_Doc;
    									
    								}
    								catch(e){
    										alert(e.message);
    									}
    								
    							}
    						return (null);
    					}
    					
    
    				function testxml(){
    						root=xml_Doc_modules.childNodes[0];
    						code_tbl=xml_Doc.createTextNode('tbl1');
    						code_text=xml_Doc_modules.createTextNode('code1');
    						root.childNodes[0].childNodes[1].appendChild(code_tbl);alert(code_tbl.nodeValue);
    						root.childNodes[0].childNodes[0].appendChild(code_text);alert(code_text.nodeValue);
    						
    						
    						}
    		</script>
    	<body>
    		<button onclick='testxml()'>Preess me</button>
    	</body>
    	</html>
    What i expect to see when I press my button is the value of my two nodes code and tbl....But the script is nt working .Somehow i managed to find out that the problem is with this line

    code_tbl=xml_Do c.createTextNod e('tbl1');


    I dnt know what's the problem,cn u enlighten me with this ???

    Thanks in adv


    With regards

    phpuser123
    Last edited by Dormilich; Apr 18 '10, 05:43 PM. Reason: please use [code] tags when posting code

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      what does the error console say?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Is the response from your url valid xml, and does it get parsed correctly?
        Do you know that root.childNodes[0].childNodes[0] exists?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          it is recommended to use xml_Doc.documen tElement to get the root. the firstChild may point to whitespace, the DTD or other processing instructions.

          Comment

          • phpuser123
            New Member
            • Dec 2009
            • 108

            #6
            That's the problem the console doesnt sayy anythng...
            I dont gt any alert box ....
            When I happen to remove line 36 and line 38,I get my nodeValue of the code...
            When i remove line 39 and 37,there is no alert box...
            R there any prob in the lines 36 and 38 ?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              then why not using document.create TextNode() ?

              Comment

              • phpuser123
                New Member
                • Dec 2009
                • 108

                #8
                Yeps i'lll try this one ...
                Bt some miracle happened,the above script started working all by itself...
                I dnt know hw this happened......

                Comment

                Working...