# 操纵 Applet 网页的 DOM

> 原文： [https://docs.oracle.com/javase/tutorial/deployment/applet/manipulatingDOMFromApplet.html](https://docs.oracle.com/javase/tutorial/deployment/applet/manipulatingDOMFromApplet.html)

每个网页都由一系列嵌套对象组成。这些对象组成了文档对象模型（DOM）。 Java applet 可以使用 [`Common DOM API`](https://docs.oracle.com/javase/8/docs/jre/api/plugin/dom/index.html) 遍历和修改其父网页的对象。

考虑一个转储其父网页内容的 Java 小程序示例。

为了遍历和操作 DOM 树，您必须首先获得对网页的`Document`对象的引用。您可以使用`com.sun.java.browser.plugin2.DOM`类中的`getDocument`方法执行此操作。这是一个代码片段，用于检索 [``DOMDump``](examples/applet_TraversingDOM/src/DOMDump.java)小程序的`start`方法中对`Document`对象的引用。请参阅代码中的内联注释。

```
public void start() {
    try {
        // use reflection to get document
        Class c =
          Class.forName("com.sun.java.browser.plugin2.DOM");
        Method m = c.getMethod("getDocument",
          new Class[] { java.applet.Applet.class });

        // cast object returned as HTMLDocument;
        // then traverse or modify DOM
        HTMLDocument doc = (HTMLDocument) m.invoke(null,
            new Object[] { this });
        HTMLBodyElement body =
            (HTMLBodyElement) doc.getBody();
        dump(body, INDENT);
    } catch (Exception e) {
        System.out.println("New Java Plug-In not available");
        // In this case, you could fallback to the old
        // bootstrapping mechanism available in the
        // com.sun.java.browser.plugin.dom package
    }
}

```

现在您已经引用了`Document`对象，您可以使用 Common DOM API 遍历和修改 DOM 树。 `DOMDump`小程序遍历 DOM 树并将其内容写入 Java 控制台日志。

```
private void dump(Node root, String prefix) {
    if (root instanceof Element) {
        System.out.println(prefix +
            ((Element) root).getTagName() + 
            " / " + root.getClass().getName());
    } else if (root instanceof CharacterData) {
        String data =
            ((CharacterData) root).getData().trim();
        if (!data.equals("")) {
            System.out.println(prefix +
                "CharacterData: " + data);
        }
    } else {
        System.out.println(prefix +
            root.getClass().getName());
    }
    NamedNodeMap attrs = root.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node attr = attrs.item(i);
            System.out.print(prefix + HALF_INDENT +
                "attribute " + i + ": " +
                attr.getNodeName());
            if (attr instanceof Attr) {
                System.out.print(" = " +
                    ((Attr) attr).getValue());
            }
            System.out.println();
        }
    }

    if (root.hasChildNodes()) {
        NodeList children = root.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                dump(children.item(i), prefix +
                    INDENT);
            }
        }
    }
}

```

在浏览器中打开 [``AppletPage.html``](examples/dist/applet_TraversingDOM/AppletPage.html)以查看正在运行的`DOMDump`小程序。检查 Java 控制台日志以获取网页 DOM 树的转储。

* * *

**Note:**  If you don't see the applet running, you need to install at least the [Java SE Development Kit (JDK) 6 update 10](http://www.oracle.com/technetwork/java/javase/downloads/index.html) release.

* * *

* * *

**Note:**  If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.

* * *

[下载 _DOM 转储 _ 示例的源代码](examplesIndex.html#ManipulatingDOM)以进一步试验。