elementlister.java

来自「随书的代码」· Java 代码 · 共 57 行

JAVA
57
字号
import org.jdom.*;import org.jdom.input.SAXBuilder;import java.io.IOException;import java.util.*;public class ElementLister {  public static void main(String[] args) {      if (args.length == 0) {      System.out.println("Usage: java ElementLister URL");       return;    }           SAXBuilder builder = new SAXBuilder();         try {      Document doc = builder.build(args[0]);      Element root = doc.getRootElement();      listChildren(root, 0);          }    // indicates a well-formedness error    catch (JDOMException e) {       System.out.println(args[0] + " is not well-formed.");      System.out.println(e.getMessage());    }      catch (IOException e) {       System.out.println(e);    }      }      public static void listChildren(Element current, int depth) {       printSpaces(depth);    System.out.println(current.getName());    List children = current.getChildren();    Iterator iterator = children.iterator();    while (iterator.hasNext()) {      Element child = (Element) iterator.next();      listChildren(child, depth+1);    }      }    private static void printSpaces(int n) {        for (int i = 0; i < n; i++) {      System.out.print(' ');     }      }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?