Here's the code

Part 1 - This class writes xml node data to a file


import org.w3c.dom.*;         // W3C DOM classes for traversing the document
import java.io.*;

/**
 * Output a DOM Level 1 Document object to a java.io.PrintWriter as a simple
 * XML document.  This class does not handle every type of DOM node, and it
 * doesn't deal with all the details of XML like DTDs, character encodings and
 * preserved and ignored whitespace.  However, it does output basic
 * well-formed XML that can be parsed by a non-validating parser.
 **/
public class XMLDocumentWriter {
    PrintWriter out;  // the stream to send output to

    /** Initialize the output stream */
    public XMLDocumentWriter(PrintWriter out) { this.out = out; }

    /** Close the output stream. */
    public void close() { out.close(); }

    /** Output a DOM Node (such as a Document) to the output stream */
    public void write(Node node) { write(node, ""); }

    /**
     * Output the specified DOM Node object, printing it using the specified
     * indentation string
     **/
    public void write(Node node, String indent) {
        // The output depends on the type of the node
        switch(node.getNodeType()) {
        case Node.DOCUMENT_NODE: {       // If its a Document node
            Document doc = (Document)node;
            out.println(indent + "<?xml version='1.0'?>");  // Output header
            Node child = doc.getFirstChild();   // Get the first node
            while(child != null) {              // Loop 'till no more nodes
                write(child, indent);           // Output node
                child = child.getNextSibling(); // Get next node
            }
            break;
        }
        case Node.DOCUMENT_TYPE_NODE: {  // It is a <!DOCTYPE> tag
            DocumentType doctype = (DocumentType) node;
            // Note that the DOM Level 1 does not give us information about
            // the the public or system ids of the doctype, so we can't output
            // a complete <!DOCTYPE> tag here.  We can do better with Level 2.
            out.println("<!DOCTYPE " + doctype.getName() + ">");
            break;
        }
        case Node.ELEMENT_NODE: {        // Most nodes are Elements
            Element elt = (Element) node;
            out.print(indent + "<" + elt.getTagName());   // Begin start tag
            NamedNodeMap attrs = elt.getAttributes();     // Get attributes
            for(int i = 0; i < attrs.getLength(); i++) {  // Loop through them
                Node a = attrs.item(i);
                out.print(" " + a.getNodeName() + "='" +  // Print attr. name
                          fixup(a.getNodeValue()) + "'"); // Print attr. value
            }
            out.println(">");                             // Finish start tag

            String newindent = indent + "    ";           // Increase indent
            Node child = elt.getFirstChild();             // Get child
            while(child != null) {                        // Loop
                write(child, newindent);                  // Output child
                child = child.getNextSibling();           // Get next child
            }

            out.println(indent + "</" +                   // Output end tag
                        elt.getTagName() + ">");
            break;
        }
        case Node.TEXT_NODE: {                   // Plain text node
            Text textNode = (Text)node;
            String text = textNode.getData().trim();   // Strip off space
            if ((text != null) && text.length() > 0)   // If non-empty
                out.println(indent + fixup(text));     // print text
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {  // Handle PI nodes
            ProcessingInstruction pi = (ProcessingInstruction)node;
            out.println(indent + "<?" + pi.getTarget() +
                               " " + pi.getData() + "?>");
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {        // Handle entities
            out.println(indent + "&" + node.getNodeName() + ";");
            break;
        }
        case Node.CDATA_SECTION_NODE: {           // Output CDATA sections
            CDATASection cdata = (CDATASection)node;
            // Careful! Don't put a CDATA section in the program itself!
            out.println(indent + "<" + "![CDATA[" + cdata.getData() +
                        "]]" + ">");
            break;
        }
        case Node.COMMENT_NODE: {                 // Comments
            Comment c = (Comment)node;
            out.println(indent + "<!--" + c.getData() + "-->");
            break;
        }
        default:   // Hopefully, this won't happen too much!
            System.err.println("Ignoring node: " + node.getClass().getName());
            break;
        }
    }

    // This method replaces reserved characters with entities.
    String fixup(String s) {
        StringBuffer sb = new StringBuffer();
        int len = s.length();
        for(int i = 0; i < len; i++) {
            char c = s.charAt(i);
            switch(c) {
            default: sb.append(c); break;
            case '<': sb.append("&lt;"); break;
            case '>': sb.append("&gt;"); break;
            case '&': sb.append("&amp;"); break;
            case '"': sb.append("&quot;"); break;
            case '\'': sb.append("&apos;"); break;
            }
        }
        return sb.toString();
    }
}


Part II - -  - This is how you can call it

doc = parser.parse(new File("parentdoc.xml"));

output(new PrintWriter(new OutputStreamWriter(System.out)));
//Output is a method that creates an instance of the writer class shown above

public static void output(PrintWriter out) {
      XMLDocumentWriter docwriter = new XMLDocumentWriter(out);
      docwriter.write(doc);
      docwriter.close();
    }

hope that helps
Santosh






Maximiliano Muller <[EMAIL PROTECTED]> on 11/30/2000 11:49:37 AM

Please respond to A mailing list about Java Server Pages specification and
      reference <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Santosh Daryani/IT/Aon Consulting)

Subject:  Using xml with jsp



Hello,

I used this code to parse an xml file:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("ejemplo.xml"));

Then I modified some nodes but the problem is that I don't know how to save
this changes.
Thanks

--

Maximiliano Muller
[EMAIL PROTECTED]

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to