- See Also:
- Top Examples, Source Code
int getLength()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node getNamedItem(String name)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node getNamedItemNS(String namespaceURI,
String localName)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node item(int index)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1590]Traverse a DOM document using Apache xerces DocumentTraversal API
By Anonymous on 2005/11/04 20:23:08 Rate
//Traverse a DOM document using Apache xerces DocumentTraversal API
public static void usingTraversal ( Document doc ) {
if ( ! ( doc instanceof DocumentTraversal ) ) {
System.out.println ( "Not DocumentTraversal" ) ;
}
DocumentTraversal traversal = ( DocumentTraversal ) doc;
NodeIterator each = traversal.createNodeIterator ( doc,NodeFilter.SHOW_ELEMENT| NodeFilter.SHOW_PROCESSING_INSTRUCTION, null, false ) ;
Node walker=null;
while ( ( walker = each.nextNode ( ) ) != null ) {
if ( walker.getNodeType ( ) == Node.PROCESSING_INSTRUCTION_NODE ) {
ProcessingInstruction pi = ( ProcessingInstruction ) walker;
if ( "aTarget".equalsIgnoreCase ( pi.getTarget ( ) ) ) {
//When we find a certain ProcessingInstruction node
//replace it with a new ProcessingInstruction node with a different target
//do something with the data
System.out.println ( pi.getData ( ) ) ;
ProcessingInstruction newPI = doc.createProcessingInstruction ( "newTarget", pi.getData ( ) ) ;
pi.getParentNode ( ) .replaceChild ( pi, newPI ) ;
}
}
if ( walker.getNodeType ( ) == Node.ELEMENT_NODE ) {
NamedNodeMap attributes = walker.getAttributes ( ) ;
Attr attr = null;
for ( int i = 0; i < attributes.getLength ( ) ; i++ ) {
attr = ( Attr ) attributes.item ( i ) ;
//do something with the data
System.out.println ( attr.getValue ( ) ) ;
}
if ( walker.getChildNodes ( ) .getLength ( ) == 1 ) {
Node aNode = walker.getFirstChild ( ) ;
if ( aNode.getNodeType ( ) == Node.TEXT_NODE ) {
//do something with the data
System.out.println ( pi.getData ( ) ) ;
}
}
}
}
}
Node removeNamedItem(String name)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node removeNamedItemNS(String namespaceURI,
String localName)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node setNamedItem(Node arg)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
Node setNamedItemNS(Node arg)
throws DOMException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples