Code example for Document

Methods: createCDATASection, createComment, createTextNode

0
            if (inCDATA) preInfo.add(new CDataInfo(str));
            else         preInfo.add(new TextInfo(str));
        } else { 
            Node n;
            if (inCDATA) n = document.createCDATASection(str);
            else         n = document.createTextNode(str);
            currentNode.appendChild(n);
        } 
    } 
 
    /** 
     * <b>SAX</b>: Implements {@link 
     * org.xml.sax.ContentHandler#characters(char[],int,int)}. 
     */ 
    public void characters(char[] ch, int start, int length)
        throws SAXException { 
        stringBuffer.append(ch, start, length);
        stringContent = true;
    } 
 
 
    /** 
     * <b>SAX</b>: Implements {@link 
     * org.xml.sax.ContentHandler#ignorableWhitespace(char[],int,int)}. 
     */ 
    public void ignorableWhitespace(char[] ch,
                                    int start,
                                    int length)
        throws SAXException { 
        stringBuffer.append(ch, start, length);
        stringContent = true;
    } 
 
    /** 
     * <b>SAX</b>: Implements {@link 
     * org.xml.sax.ContentHandler#processingInstruction(String,String)}. 
     */ 
    public void processingInstruction(String target, String data)
        throws SAXException { 
        if (inDTD)
            return; 
 
        appendStringData(); // Add any collected String Data before PI 
 
        if (currentNode == null)
            preInfo.add(new ProcessingInstructionInfo(target, data));
        else 
            currentNode.appendChild
                (document.createProcessingInstruction(target, data));
    } 
 
    // LexicalHandler ///////////////////////////////////////////////////////// 
 
    /** 
     * <b>SAX</b>: Implements {@link 
     * org.xml.sax.ext.LexicalHandler#startDTD(String,String,String)}. 
     */ 
    public void startDTD(String name, String publicId, String systemId)
        throws SAXException { 
        appendStringData(); // Add collected string data before entering DTD 
        inDTD = true;
    } 
 
    /** 
     * <b>SAX</b>: Implements {@link org.xml.sax.ext.LexicalHandler#endDTD()}. 
     */ 
    public void endDTD() throws SAXException { 
        inDTD = false;
    } 
 
    /** 
     * <b>SAX</b>: Implements 
     * {@link org.xml.sax.ext.LexicalHandler#startEntity(String)}. 
     */ 
    public void startEntity(String name) throws SAXException {
    } 
 
    /** 
     * <b>SAX</b>: Implements 
     * {@link org.xml.sax.ext.LexicalHandler#endEntity(String)}. 
     */ 
    public void endEntity(String name) throws SAXException {
    } 
 
    /** 
     * <b>SAX</b>: Implements {@link 
     * org.xml.sax.ext.LexicalHandler#startCDATA()}. 
     */ 
    public void startCDATA() throws SAXException { 
        appendStringData(); // Add any collected String Data before CData 
        inCDATA       = true;
        stringContent = true; // always create CDATA even if empty.
    } 
 
    /** 
     * <b>SAX</b>: Implements {@link 
     * org.xml.sax.ext.LexicalHandler#endCDATA()}. 
     */ 
    public void endCDATA() throws SAXException { 
        appendStringData(); // Add the CDATA section 
        inCDATA = false;
    } 
 
    /** 
     * <b>SAX</b>: Implements 
     * {@link org.xml.sax.ext.LexicalHandler#comment(char[],int,int)}. 
     */ 
    public void comment(char[] ch, int start, int length) throws SAXException {
        if (inDTD) return;
        appendStringData(); 
 
        String str = new String(ch, start, length);
        if (currentNode == null) {
            preInfo.add(new CommentInfo(str));
        } else { 
            currentNode.appendChild(document.createComment(str));
        } 
    } 
}