KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > org > w3c > dom > Element

org.w3c.dom
Interface Element

All Superinterfaces:
Node
All Known Implementing Classes:
IIOMetadataNode
See Also:
Top Examples, Source Code

String getAttribute(String name)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1582]Get a specific attritbute fpor all nodes
By Anonymous on 2005/11/04 20:23:08  Rate
Element aElement = ... //from somewhere 
  
  
 NodeList nodeList = aElement.getElementsByTagName  ( "aTag" ) ; 
 int length = nodeList.getLength  (  ) ; 
 for  ( int r = 0; r  <  length; ++r )  
     System.out.println  (   (  ( Element )  nodeList.item  ( r )  ) .getAttribute  ( "aAttr" )  ) ; 
  } 


[1588]Find a node and remove a child
By Anonymous on 2005/11/04 20:23:08  Rate
//Getting Element e from somewhere ... then ... 
 Document doc = e.getOwnerDocument (  ) ; 
  
  
 //make a DEEP copy of DOM tree 
 Document workspace =  ( Document )  doc.cloneNode ( true ) ; 
  
  
 Element e1 = workspace.getElementById ( "someAttr" ) ; 
 workspace.getDocumentElement (  ) .removeChild ( e1 ) ; 
 


Attr getAttributeNode(String name)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Attr getAttributeNodeNS(String namespaceURI,
                        String localName)
                        throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


String getAttributeNS(String namespaceURI,
                      String localName)
                      throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


NodeList getElementsByTagName(String name)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


NodeList getElementsByTagNameNS(String namespaceURI,
                                String localName)
                                throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


TypeInfo getSchemaTypeInfo()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


String getTagName()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean hasAttribute(String name)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean hasAttributeNS(String namespaceURI,
                       String localName)
                       throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void removeAttribute(String name)
                     throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Attr removeAttributeNode(Attr oldAttr)
                         throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void removeAttributeNS(String namespaceURI,
                       String localName)
                       throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setAttribute(String name,
                  String value)
                  throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Attr setAttributeNode(Attr newAttr)
                      throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Attr setAttributeNodeNS(Attr newAttr)
                        throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setAttributeNS(String namespaceURI,
                    String qualifiedName,
                    String value)
                    throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setIdAttribute(String name,
                    boolean isId)
                    throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setIdAttributeNode(Attr idAttr,
                        boolean isId)
                        throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setIdAttributeNS(String namespaceURI,
                      String localName,
                      boolean isId)
                      throws DOMException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1618]Parse and print a formated XML file
By Anonymous on 2005/11/04 20:23:08  Rate
import java.io.*; 
 import javax.xml.parsers.*; 
 import org.w3c.dom.*; 
 import org.w3c.dom.traversal.*; 
  
  
 /** 
 This application class accepts a source XML filename and uses SAX to parse 
 the file. It echoes the contents with some modifications, restoring 
 line breaks and a simple pattern of indenting child elements. 
  
  
 One major limitation: we gather character data for elements in a single 
 buffer, so mixed-content documents will lose a lot of data! This works 
 best with data-centric documents where elements either have single values 
 or child elements, but not both. 
 */
 
 public class XMLOutput 
  {  
     /** 
     Builds a DOM document, checking that the DOM2 Traversal feature is 
     supported before proceeding. Uses a  < code > TreeWalker < /code >  to pass 
     through the source document, replacing the various SAX handlers from 
     the previous version with a single loop that keeps element context as 
     it goes. Writes the formatted output to a string buffer and from there 
     to the console. 
     */
 
     public static void main  ( String [  ]  args )  
      {  
         try 
          {  
             DocumentBuilder parser = DocumentBuilderFactory.newInstance  (  )  
                 .newDocumentBuilder  (  ) ; 
             if  ( !parser.getDOMImplementation  (  )  
                 .hasFeature  ( "Traversal", "2.0" )  )  
              {  
                 System.out.println  ( "This application requires a DOM parser " 
                     + "that supports traversal." ) ; 
                 System.exit  ( -1 ) ; 
              }  
  
  
             StringBuffer indent = new StringBuffer  (  ) ; 
             StringBuffer output = new StringBuffer  (  ) ; 
  
  
             Document doc =  ( args.length == 0 )  
                 ? parser.parse  ( System.in )  
                 : parser.parse  ( new File  ( args [ 0 ]  )  ) ; 
  
  
             System.out.println  ( " < ?xml version=\"1.0\" encoding=\"UTF-8\" ? > " ) ; 
             TreeWalker walker =  (  ( DocumentTraversal )  doc ) .createTreeWalker 
                  ( doc.getDocumentElement  (  ) , 
                     NodeFilter.SHOW_ELEMENT, null, false ) ; 
             boolean goingUp = false; 
             Element element = null; 
             boolean completeElement = false; 
             boolean justWentUp = false; 
             while  ( walker != null )  
              {  
                 element =  ( Element )  walker.getCurrentNode  (  ) ; 
  
  
                 // Write start tag and attributes: 
                 if  ( !goingUp )  
                  {  
                     output.append  ( endLine )  
                           .append  ( indent )  
                           .append  ( ' < ' )  
                           .append  ( element.getTagName  (  )  ) ; 
  
  
                     NamedNodeMap attributes = element.getAttributes  (  ) ; 
                     int length = attributes.getLength  (  ) ; 
                     for  ( int a = 0; a  <  length; ++a )  
                         output.append  ( endLine )  
                               .append  ( indent )  
                               .append  ( standardIndent )  
                               .append  (  (  ( Attr )  attributes.item  ( a )  )  
                                           .getName  (  )  )  
                               .append  ( "=\"" )  
                               .append  (  (  ( Attr )  attributes.item  ( a )  )  
                                           .getValue  (  )  )  
                               .append  ( '\"' ) ; 
  
  
                     if  ( length  >  0 )  
                         output.append  ( endLine )  
                               .append  ( indent ) ; 
  
  
                     output.append  ( ' > ' ) ; 
  
  
                     indent.append  ( standardIndent ) ; 
                  }  
  
  
                 // Walk to next node, either down, over, or up: 
                 justWentUp = goingUp; 
                 completeElement = false; 
                 if  ( goingUp || walker.firstChild  (  )  == null )  
                  {  
                     goingUp = false; 
                     completeElement = true; 
                     if  ( walker.nextSibling  (  )  == null )  
                      {  
                         goingUp = true; 
                         if  ( walker.parentNode  (  )  == null )  
                             walker = null; 
                      }  
                  }  
  
  
                 // Write contents and end tag: 
                 if  ( completeElement )  
                  {  
                     if  ( !justWentUp )  
                         output.append  ( escape 
                              (  (  ( Text )  element.getFirstChild  (  )  ) .getData  (  )  )  ) ; 
  
  
                     indent.delete  ( indent.length  (  )  - standardIndent.length  (  ) , 
                         indent.length  (  )  ) ; 
  
  
                     if  ( justWentUp )  
                         output.append  ( endLine )  
                               .append  ( indent ) ; 
  
  
                     output.append  ( " < /" )  
                           .append  ( element.getTagName  (  )  )  
                           .append  ( ' > ' ) ; 
                  }  
              }  
             System.out.println  ( output.toString  (  )  ) ; 
          }  
         catch  ( Exception ex )  
          {  
             ex.printStackTrace  ( System.err ) ; 
          }  
      }  
  
  
     /** 
     Filter to pass strings to output, escaping  < b > < < /b >  and  < b > & < /b >  
     characters to &lt; and &amp; respectively. 
     */
 
     private static String escape  ( String source )  
      {  
         StringBuffer result = new StringBuffer  (  ) ; 
         byte [  ]  buffer = source.getBytes  (  ) ; 
         for  ( int c = 0; c  <  buffer.length; ++c )  
             if  ( buffer [ c ]  == ' < ' )  
                 result.append  ( "<" ) ; 
             else if  ( buffer [ c ]  == '&' )  
                 result.append  ( "&" ) ; 
             else 
                 result.append  (  ( char )  buffer [ c ]  ) ; 
  
  
         return result.toString  (  ) ; 
      }  
  
  
     private static final String endLine = "\r\n"; 
     private static final String standardIndent = " "; 
  }  
  
  
 

Popular Tags