Forum Stats

  • 3,875,272 Users
  • 2,266,902 Discussions
  • 7,912,132 Comments

Discussions

Looking for a Generic_node-to-string utility

843834
843834 Member Posts: 49,972
edited Jul 18, 2001 11:57AM in Java Technology & XML
Hello

I'm creating a "SurveyEngine" application that will use a XML document defining the questions and storing the answers of the users. There is part of my webApp that must:
1-parse the XML file (i am ok till there)
2-append a XML child node (.. it should not be too hard, but if you have sample codes, please send)
3-translate the modified document object back to a string to overwrite the file

I need help mostly for the 3th step. I looked in the java API in the packages dom, sax, etc... and I didnt find anything. Could someone tell me which package.class I must use? If you know that such a class does not exist in the java API, but that you already defined a class to handle generic_node-to-string translation, I would be very grateful if you could send me your sources.

Thanks in advance,
Alexandre Bouchard

Comments

  • 843834
    843834 Member Posts: 49,972
    I recommend taking a look at "java and xml" by Brett McLaughlin. He does exactly what you're trying to do in chapter 12 of this book. You can get sample code at:
    http://examples.oreilly.com/javaxml/
    The examples zip file contains code as well, although I'm not sure how useful it is without the book.
    However, for your question 3), you might want to take a look at the class org.apache.xml.serialize.XMLSerializer which does what you want. This comes with xerces, which you can download at xml.apache.org. Take a look at the documentation of this class.

    Hope that helps.
  • 843834
    843834 Member Posts: 49,972
    yah.. thx

    But what I have is a DOM three, I did not use SAX parsing. Serializer works with SAX.. So I need something that work w/ a DOM Document
  • 843834
    843834 Member Posts: 49,972
    Perhaps you could tell us what parser you're using. Even if you're building a DOM tree your parser most likely uses SAX for parsing XML, but that's a side point. org.apache.xml.serialize.XMLSerializer supports both SAX and DOM pretty serializing.
  • 843834
    843834 Member Posts: 49,972
    ok here is the parsing section of my code:
    	    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	    
           	//basic response configurations
    		response.setContentType("text/html");
    		out = response.getWriter();  
    				    
    	    try 
    	    {
    	            
                //create the DOM parser and parse the XML docs
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse( new File("documents.xml") );
    
    
                
                ...here there will be modifications done to document
                ... and here, I want to ouput the modified document to the screen or to a file or whatever...                                                       
            } 
            catch (SAXException sxe) 
            { // Error generated during parsing) 
                    Exception x = sxe; 
                    if (sxe.getException() != null) 
                            x = sxe.getException(); 
                    x.printStackTrace();
                    out.println( "SAX Exception: " + sxe ); 
            } 
            catch (ParserConfigurationException pce) 
            { // Parser with specified options can't be built 
                    pce.printStackTrace();
                    out.println( "ParserConfigurationException: " + pce );
            } 
            catch (IOException ioe) 
            { // I/O error 
                    ioe.printStackTrace();
                    out.println( "io Exception: " + ioe );
            }
    All I want is a kind of document.toString() that really transform the document to a string. If you could write me some codes that do that using XMLSerializer, I would be very grateful.

    I know that it wouldnt be too hard to write my own XMLToString() utility, but it would violate my First Law of Programming: Never reinvent the wheel.

    Thanks a lot for your attention lk555
  • 843834
    843834 Member Posts: 49,972
    You may want to check out JDOM.... www.jdom.org the APIs are simple and its just about all you need for simple xml
  • 843834
    843834 Member Posts: 49,972
    Thx for the advice!

    JDOM is far better than org.dom for my needs!
This discussion has been closed.