XML document object model (DOM) class

the xml document object model(dom) class is an in-memory representation of an xml document.the dom allows you to programmatically read,manipulate, and modify an xml document.

here we can discuss about some xml dom operations.

the xml getelementsbytagname() method is used under some examples.


look the xml file,which used in all examples

       <?xml  version="1.0"   encoding="utf-8" ?>
                        
                          <books>
                          
                              <book>
                              
                                   <author> sandeep </author>
                                   
                                   <price   format="dollar">34.34</price>
                                   
                                   <date>05/09/2012</date>
                                   
                              </book>
                              
                              <pubinfo>
                                
                                      <publisher>apress</publisher>
                                      
                                      <state>unitedstate</state>
                                      
                              <pubinfo>
                                            
                      </books>

how get the value of an element

the following example retrieves the text value of the <author> and <price> element.

                                
 var  author_text  =  xmldoc.getelementsbytagname("author")[0].childnodes[0]

.nodevalue; var price_text = xmldoc.getelementsbytagname("price")[0].childnodes[0]

.nodevalue;

how get the value of an attributes

the following example retrieves the text value of the "format" attribute of the <price> element.

 var  price_attribute_text  =  xmldoc.getelementsbytagname("price").

getattribute("format");

how change the value of an element

the following example change the text value of the <author> and <price> element.

                                
 var  author  =  xmldoc.getelementsbytagname("author"); 

      author.nodevalue="ramkumar sing ji" ; 
     
 var  price  =  xmldoc.getelementsbytagname("price"); 
      
      price.nodevalue="4440.30" ;                       

how remove an element

the following example removes the first node in the <book> element.

                                
   var  author  =  xmldoc.getelementsbytagname("book")[0]; 

        author.removechild(author.childnodes[0]);     

if you want to learn our complete xml dom tutorial, please click on this link xml dom tutoril