|
-
Aug 31st, 2001, 04:27 PM
#1
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
-
Aug 31st, 2001, 08:17 PM
#2
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.
-
Aug 31st, 2001, 08:24 PM
#3
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
-
Sep 1st, 2001, 11:42 PM
#4
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.
-
Sep 1st, 2001, 11:49 PM
#5
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
-
Sep 2nd, 2001, 12:54 AM
#6
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
-
Sep 2nd, 2001, 02:13 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|