Changeset 12417


Ignore:
Timestamp:
2006-08-08T17:07:59+12:00 (18 years ago)
Author:
shaoqun
Message:

added methods for printing out xml nodes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r11630 r12417  
    290290    }
    291291    }
     292
     293    public static void printXMLNode(Node e) {
     294    printXMLNode(e, 0) ;
     295    }
     296 
     297 public static void printXMLNode(Node e, int depth) { //recursive method call using DOM API...
     298   
     299    for (int i=0 ; i<depth ; i++)
     300        System.out.print(' ') ;
     301
     302    if (e.getNodeType() == Node.TEXT_NODE){
     303        System.out.println("text") ;
     304        //System.out.println(e.getNodeValue()) ;
     305        return ;
     306    }
     307   
     308    System.out.print('<');
     309    System.out.print(e.getNodeName());
     310    NamedNodeMap attrs = e.getAttributes();
     311    for (int i = 0; i < attrs.getLength(); i++) {
     312        Node attr = attrs.item(i);
     313        System.out.print(' '); 
     314        System.out.print(attr.getNodeName());
     315        System.out.print("=\"");
     316        System.out.print(attr.getNodeValue());   
     317        System.out.print('"');
     318    }
     319
     320    NodeList children = e.getChildNodes();
     321
     322    if (children == null || children.getLength() == 0)
     323        System.out.println("/>") ;
     324    else {
     325       
     326        System.out.println('>') ;
     327       
     328        int len = children.getLength();
     329        for (int i = 0; i < len; i++) {
     330        printXMLNode(children.item(i), depth + 1);
     331        }
     332       
     333        for (int i=0 ; i<depth ; i++)
     334        System.out.print(' ') ;
     335       
     336        System.out.println("</" + e.getNodeName() + ">");
     337    }   
     338
     339    }
    292340}
Note: See TracChangeset for help on using the changeset viewer.