XMLC Tutorial: Grouping Document Elements

Contents

  1. Using XMLC for multiple changes
  2. A Sample HTML Page
  3. The Associated Document Object Model (DOM)
  4. Resulting Java Class
  5. Usage from a Manipulation Java Class
  6. Resulting HTML

Using XMLC for multiple changes

This chapter will show how XMLC may be used to make a particular change at several locations throughout a document. There are several ways to do this. We will demonstrate two of them: changing all tags of a particular type, and changing all tags with a comman name attribute value.

A Sample HTML Page

The following sample HTML contains several <h2> tags, and several tags with a <name> attribute.


<HTML>
<HEAD>
    <TITLE>Hello, World</TITLE>
</HEAD>

<BODY BGCOLOR=#FFFFFF TEXT="#000000">

<H1>Hello, World</H1>

<H2>A subheader</H2>

We want to <FONT COLOR='00FF00' NAME='highlight'>highlight</FONT> part of
this sentence. 

<H2>Another subheader</H2>

In this sentence, we display the colors <FONT COLOR='FF0000'>red</FONT>,
<FONT COLOR='00FF00'>green</FONT> and <FONT COLOR='0000FF'>blue</FONT>. 

<P>
The previous sentence <FONT COLOR='00FF00' NAME='highlight'>did not use
color to highlight text</FONT>!

</BODY>
</HTML>

The Associated Document Object Model (DOM)

XMLC generates the following Document Object Model (DOM) for the above HTML.


DOM hierarchy:
    BasicHTMLDocument
        BasicHTMLHtmlElement: html
            BasicHTMLHeadElement: head
                BasicHTMLTitleElement: title
                    BasicText: text=Hello, World
            BasicHTMLBodyElement: body: bgcolor='#FFFFFF' text='#000000'
                BasicHTMLHeadingElement: h1
                    BasicText: text=Hello, World
                BasicHTMLHeadingElement: h2
                    BasicText: text=A subheader
                BasicText: text=We want to 
                BasicHTMLFontElement: font: color='00FF00' name='highlight'
                    BasicText: text=highlight
                BasicText: text= part of this sentence.
                BasicHTMLHeadingElement: h2
                    BasicText: text=Another subheader
                BasicText: text=In this sentence, we display the colors 
                BasicHTMLFontElement: font: color='FF0000'
                    BasicText: text=red
                BasicText: text=, 
                BasicHTMLFontElement: font: color='00FF00'
                    BasicText: text=green
                BasicText: text= and 
                BasicHTMLFontElement: font: color='0000FF'
                    BasicText: text=blue
                BasicText: text=.
                BasicHTMLParagraphElement: p
                    BasicText: text=The previous sentence 
                    BasicHTMLFontElement: font: color='00FF00' name='highlight'
                        BasicText: text=did not use color to highlight text
                    BasicText: text=!

Resulting Java Class

XMLC generates the following Java class for the above HTML. Unlike the <id> attribute, XMLC does not special Java code for the <name>. However, we can use methods defined in W3C's Document Object Model API.


/*
 ************************************
 * XMLC GENERATED CODE, DO NOT EDIT *
 ************************************
 */
import org.w3c.dom.*;
import org.enhydra.xml.xmlc.XMLCUtil;
public class demo_grouping extends org.enhydra.xml.xmlc.html.HTMLObject {
    private static Document protoDocument;
    static private void initProtoDom() {
        Node $node0, $node1, $node2, $node3, $node4;
        Element $elem0, $elem1, $elem2, $elem3;
        Attr $attr0, $attr1, $attr2, $attr3;

        com.docuverse.dom.DOM $$dom = new com.docuverse.dom.DOM();
        com.docuverse.dom.html.HTMLFactory $$factory = new com.docuverse.dom.html.HTMLFactory();
        $$dom.setFactory($$factory);
        protoDocument = (Document)$$factory.createDocument($$dom, "HTML");
        
        $elem0 = protoDocument.createElement("html");
        protoDocument.appendChild($elem0);
        
        $elem1 = protoDocument.createElement("head");
        $elem0.appendChild($elem1);
        
        $elem2 = protoDocument.createElement("title");
        $elem1.appendChild($elem2);
        
        $node3 = protoDocument.createTextNode("Hello, World");
        $elem2.appendChild($node3);
        
        $elem1 = protoDocument.createElement("body");
        $elem0.appendChild($elem1);
        
        $attr1 = protoDocument.createAttribute("text");
        $attr1.setValue("#000000");
        $elem1.setAttributeNode($attr1);
        
        $attr1 = protoDocument.createAttribute("bgcolor");
        $attr1.setValue("#FFFFFF");
        $elem1.setAttributeNode($attr1);
        
        $elem2 = protoDocument.createElement("h1");
        $elem1.appendChild($elem2);
        
        $node3 = protoDocument.createTextNode("Hello, World");
        $elem2.appendChild($node3);
        
        $elem2 = protoDocument.createElement("h2");
        $elem1.appendChild($elem2);
        
        $node3 = protoDocument.createTextNode("A subheader");
        $elem2.appendChild($node3);
        
        $node2 = protoDocument.createTextNode("We want to ");
        $elem1.appendChild($node2);
        
        $elem2 = protoDocument.createElement("font");
        $elem1.appendChild($elem2);
        
        $attr2 = protoDocument.createAttribute("name");
        $attr2.setValue("highlight");
        $elem2.setAttributeNode($attr2);
        
        $attr2 = protoDocument.createAttribute("color");
        $attr2.setValue("00FF00");
        $elem2.setAttributeNode($attr2);
        
        $node3 = protoDocument.createTextNode("highlight");
        $elem2.appendChild($node3);
        
        $node2 = protoDocument.createTextNode(" part of this sentence.");
        $elem1.appendChild($node2);
        
        $elem2 = protoDocument.createElement("h2");
        $elem1.appendChild($elem2);
        
        $node3 = protoDocument.createTextNode("Another subheader");
        $elem2.appendChild($node3);
        
        $node2 = protoDocument.createTextNode("In this sentence, we display the colors ");
        $elem1.appendChild($node2);
        
        $elem2 = protoDocument.createElement("font");
        $elem1.appendChild($elem2);
        
        $attr2 = protoDocument.createAttribute("color");
        $attr2.setValue("FF0000");
        $elem2.setAttributeNode($attr2);
        
        $node3 = protoDocument.createTextNode("red");
        $elem2.appendChild($node3);
        
        $node2 = protoDocument.createTextNode(", ");
        $elem1.appendChild($node2);
        
        $elem2 = protoDocument.createElement("font");
        $elem1.appendChild($elem2);
        
        $attr2 = protoDocument.createAttribute("color");
        $attr2.setValue("00FF00");
        $elem2.setAttributeNode($attr2);
        
        $node3 = protoDocument.createTextNode("green");
        $elem2.appendChild($node3);
        
        $node2 = protoDocument.createTextNode(" and ");
        $elem1.appendChild($node2);
        
        $elem2 = protoDocument.createElement("font");
        $elem1.appendChild($elem2);
        
        $attr2 = protoDocument.createAttribute("color");
        $attr2.setValue("0000FF");
        $elem2.setAttributeNode($attr2);
        
        $node3 = protoDocument.createTextNode("blue");
        $elem2.appendChild($node3);
        
        $node2 = protoDocument.createTextNode(".");
        $elem1.appendChild($node2);
        
        $elem2 = protoDocument.createElement("p");
        $elem1.appendChild($elem2);
        
        $node3 = protoDocument.createTextNode("The previous sentence ");
        $elem2.appendChild($node3);
        
        $elem3 = protoDocument.createElement("font");
        $elem2.appendChild($elem3);
        
        $attr3 = protoDocument.createAttribute("name");
        $attr3.setValue("highlight");
        $elem3.setAttributeNode($attr3);
        
        $attr3 = protoDocument.createAttribute("color");
        $attr3.setValue("00FF00");
        $elem3.setAttributeNode($attr3);
        
        $node4 = protoDocument.createTextNode("did not use color to highlight text");
        $elem3.appendChild($node4);
        
        $node3 = protoDocument.createTextNode("!");
        $elem2.appendChild($node3);
        
    }

    static {
        initProtoDom();
    }

    public demo_grouping() {
        super(protoDocument);
    }


}

Usage from a Manipulation Java Class


import org.w3c.dom.*;
import org.w3c.dom.html.*;

public class demo_grouping_creator {

    private static void centerSubheaders( NodeList nodeList ){

        int length = nodeList.getLength(); 
        int i ; 

        for( i=0 ; i<length ; i++ ){ 
            HTMLElement element = (HTMLElement) nodeList.item( i ); 
            element.setAttribute( "ALIGN", "CENTER" ); 
        }

    }

    private static void alterHighlighting( NodeList nodeList ){

        int length = nodeList.getLength(); 
        int i ; 

        for( i=0 ; i<length ; i++ ){ 
            HTMLElement element = (HTMLElement) nodeList.item( i ); 
            element.removeAttribute( "COLOR" ); 
            element.setAttribute( "SIZE", "+2" ); 
        }

    }
   
    public static void main (String[] args) {
    
        // Create an instance of the HTML page object.
        demo_grouping grouping = new demo_grouping();
    
        // Get a list of all H2 elements
        NodeList subheaderElements = grouping.getElementsByTagName( "H2" ); 
    
        // Add an 'ALIGN=CENTER' attribute to these elements
        centerSubheaders( subheaderElements ); 
    
        // Get a list of all nodes with name 'highlight'
        NodeList highlightElements = grouping.getElementsByName( "highlight" ); 
    
        // In these elements, replace the COLOR attribute with a FONT attribute
        alterHighlighting( highlightElements ); 
    
        // Print out the results.
        System.out.print( grouping.toString() );
    
    }

}

In the above Java, the highlighted code shows how to extract a NodeList of DOM elements. The first highlighted code shows how to find all tags of a given type, in this case the <h2> tag. The second highlighted section shows how to extract all tags with a given value for the <name> attribute, in this case <name="highlight">.

The two methods centerSubheaders() and alterHighlighting() show how to scan a NodeList, processing each element in turn, and how to manipulate the attributes of a given element.

It is possible to do much more sophisticated processing of the DOM, but that goes beyond the scope of this document. We suggest you peruse the Java DOM API javadoc.

Resulting HTML

Here is the HTML page created by the above example:


<html>
<head>
    <title>Hello, World</title>
</head>

<body text='#000000' bgcolor='#FFFFFF'>

<h1>Hello, World</h1>

<h2 ALIGN='CENTER'>A subheader</h2>

We want to <font name='highlight'
SIZE='+2'>highlight</font> part of this sentence.

<h2 ALIGN='CENTER'>Another subheader</h2>

In this sentence, we display the colors <font color='FF0000'>red</font>,
<font color='00FF00'>green</font> and <font color='0000FF'>blue</font>.
<p>
The previous sentence <font name='highlight' SIZE='+2'>did not use color to
highlight text</font>!
</p>

</body>
</html>

Once again, the whitespace in the above example HTML is added by hand. XMLC removes as much whitespace as possible.