Results 1 to 7 of 7

Thread: XML output...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Smile XML output...

    I want a servlet to output XML data. Anybody know how to do that?

    Thanks
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    VirtuallyVB
    Guest

    Lightbulb

    Thanks for reminding me that I have another book to read. I got it in a sweet bundle when I was going for my certification. I didn't even remember that it came with a cd. It's called "JAVA DEVELOPER'S GUIDE to Servlets and JSP", Bill Brogden from SYBEX, ISBN 0-7821-2809-2

    This is just a crack at helping you because I've only created and executed a basic servlet and JSP and never composed or manipulated an XML file.

    A skeleton of a method to read from a (XML?) file using utility classes from the
    com.sun.xml.parser package to return a
    com.sun.xml.tree.XmlDocument object:

    public XmlDocument exampleDOM(String src){
    File xmlFile = new File(src);
    try{
    InputSource input = Resolver.createInputSource(xmlFile);
    // ... the "false" flag says not to validate
    XmlDocument doc = XmlDocument.createXmlDocument(input, false);
    return doc;
    }catch(SAXParseException spe){
    //handle it
    }catch(SAXException se){
    //handle it
    }catch(IOException ie){
    //handle it
    }
    return null;
    }

    In the next pages it mentions obtaining a NodeList with all elements named "Book" in a document:
    Element E = doc.getDocumentElement();
    NodeList vList = E.getElementsByTagName("Book");

    Disclaimer: I've never coded for XML, I've only read about its capabilities for J2EE and EJB's. Hmmm, I'd have to locate another book that I'm sure told me how to access an XML element and its attributes, but that was for EJB's.

    Let me know if I'm in the ball-park.

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    thanks, I'll have to keep that code in mind, but what about if I am taking data from a db, and I want to generate a dynamic XML page from the data?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    VirtuallyVB
    Guest

    Question

    Isn't that the same as writing a text file or HTML file? Are you sure you are dealing with an XML file which is just a text file?

    "Dynamic" XML? Are you sure you don't mean DHTML?

    Okay, at least one person (or company--IBM) is putting the word "dynamic" before "XML".
    http://www.alphaworks.ibm.com/formula/dynamicxmlforjava

    But then again, they define something new called DXMLJ, not XML per se.

  5. #5

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Talking

    What I mean to say is...

    I have info coming from a database. If I was to output it as HTML I would loop through every record and output all the HTML formatting info with the data.

    I would rather write up an XSL stylesheet and loop through the data from the db and output it like this:

    <?xml version="1.0"?>
    stylesheet info
    <item>
    <sub-item/>
    <sub-item/>
    <sub-item/>
    </item>

    that way the browser would have to do the work to format the data, and not the servlet. As it stands, I can only get the browser to recognize the above structure as HTML, not XML.

    I also tried several ways of changing the content header of the response, but it did not work.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Well, seems changing the Content header to "text/XML" now works.

    Thanks for your help

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    VirtuallyVB
    Guest
    Ahh, the elusive content header.

    So you were just writing text.

    You're welcome. You set a new direction for me (before my book gets outdated).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width