Code example for Document

Methods: appendChild, createTextNode

0
            public void endElement(String uri, String localName, String qName) {
                addTextIfNeeded(); 
                Element closedEl = elementStack.pop();
                closedEl.setAttribute(lineEndNumAttribName, String.valueOf(locator.getLineNumber()));
                if (elementStack.isEmpty()) { // Is this the root element?
                    doc.appendChild(closedEl);
                } else { 
                    Element parentEl = elementStack.peek();
                    parentEl.appendChild(closedEl);
                } 
            } 
 
            @Override 
            public void characters(char ch[], int start, int length) throws SAXException {
                textBuffer.append(ch, start, length);
            } 
 
            // Outputs text accumulated under the current node 
            private void addTextIfNeeded() { 
                if (textBuffer.toString().trim().length() > 0) {
                    Element el = elementStack.peek();
                    Node textNode = doc.createTextNode(textBuffer.toString());
                    el.appendChild(textNode);
                } 
                textBuffer.delete(0, textBuffer.length());
            } 
        };