How to Manipulate DOM of Applet's Web Page?
Hi Friends,
Last time you all guys helped me a lot for solving my issue in Java coding. Now I am working on an applets. I am having a doubt about the manipulating DOM of applets web page.!! So thought that you guys will help me again. So please explain me how to Manipulate DOM of Applet's Web Page? If possible please provide me the necessary coding by which it would be easy for me to understand.!! Thanks a Lot in Advance.!! :rolleyes:
Re: How to Manipulate DOM of Applet's Web Page?
Before using the DOM of Applet's Web Page, I think that you should know some basic things that are very important from the Java's point of view. The Java DOM API is an implementation of the Java bindings specified in Document Object Model (DOM). In this, only the core DOM interfaces are implemented. Every web page is composed of a series of nested objects. These objects make up the Document Object Model (DOM). The primary purpose of the Java DOM API is to allow Java Applets and Pluglets to interact with and modify the document they are embedded into. Hope that you got some points from this post.!!
Re: How to Manipulate DOM of Applet's Web Page?
The Java DOM API is written as a very thin layer on top of the C++ coreDOM API using JNI. The DOM API provides a standardized, versatile view of a document's contents. By supporting the DOM API, a program allows its data to be manipulated by other routines. Hence, each Java object delegates all method calls to the corresponding C++ coreDOM object. You can use the DOM calls for solving a very wide of range of problems, but it is not necessary that it can solve all the problems. You can say a DOM implementation as a software which takes the parsed XML or HTML document and makes it available for processing via the DOM interfaces.
Re: How to Manipulate DOM of Applet's Web Page?
I have provided you with an example of coding that can retrieve a reference to a Document object in the DOMDump applet's start method. The following is the sample of the coding :
Code:
public void start() {
try {
Class c = Class.forName("URL goes here");
Method mtd = c.getMethod("getDocument",
new Class[] { java.applet.Applet.class });
HTMLDocument doc = (HTMLDocument) mtd.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");
}
}
Re: How to Manipulate DOM of Applet's Web Page?
I know how to use DOM in HTML. So I am providing the same so that it may help you. There are two ways of using the HTML module of the DOM. The following are the two ways :
- You may write the script of JavaScript or the VBScript on your page.
- The other way is by using an external application such as a plug-in, ActiveX control, or Java applet, that accesses the document through your browser.
Also the good point is that DOM can be implemented using CORBA, COM, or Java Virtual Machine runtime bindings.
Re: How to Manipulate DOM of Applet's Web Page?
The DOMDump applet traverses the DOM tree and writes its contents to the Java Console log. You can check this in following code :
Code:
private void DOMdump(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);
}
}
}
}