Using JSP / Java to write/create an xml file

I have been looking around the internet and in my books and I cannot find a good example of JAVA/JSP writing an XML file. I already have a recursive method that will generate the xml .. what's missing in my code now is how am i goin to append or write that xml string into an existing file..
suppose i have this xml string
String xmlString = "<?xml version="1.0"?><txn><test>this is just a test</test></txn>";
FileOutputStream fos = new FileOutPutStream(C://files/sample.xml);
now , how i will write the xmlString into sample.xml file...
Any suggestions/ ideas/ tutorial links would be deeply appreciated.. thx!

never mind.. my code is workin now..

Similar Messages

  • Do all JSP/Java bean webapps need an xml file?

    Hi all,
    I have a jsp/ java bean webb application. This is being run on Resin at
    Lunarpages.
    The site has been shut down due to 'class-loader' not found. I was told it
    was causing problems for other java users on the server.
    I realise this is limited information but any insight would be welcome.
    This is our first JSP / java bean web app.
    Also if anyone could tell me briefly the purpose of an xml document in a JSP/Java bean webapplication I would greatly appreciate it.
    Thanks in advance
    Regards
    Jim

    Hi there,
    Thanks for the reply.
    Our application allows people to purchase wedding pictures online. It ran locally on resin we then had to change all the relative file paths in order to deploy it on lunarpages.
    Lunarpages use resin also.
    We had the application tested and working on Lunarpages for the last week .This morning they sent us a message saying they had shut our JSP pages down as our application was causing problems for other java users. errant code unknown element ' class-loader' .
    Not too much help from lunarpages...
    thanks

  • HO w to use SAX parser to create an XML file on the fly

    Hi All,
    Currently I am using the DOM parser to create an XML file from a text file. But as the DOM takes much memory and inefficient, I need to convert the DOM translator to SAX translator. Can I do that ?? If YES then how to go about that and if NO then what may be the workaround for that.
    Please help me out
    Thanx in advance
    kaushik

    Incidentally, look at this thread:
    http://forum.java.sun.com/thread.jsp?forum=34&thread=252415
    It has an example of how to transform an XML file via XSLT. If you change this to use the zero-argument form of newTransformer, it will apply the "identity transformation" to your input, thus outputting your XML in valid form. Now you just need to figure out how to provide SAX input to this, and the JAXP download includes an example of that.

  • Creating an XML file using Java Standalone

    Hi,
    My problem is -
    1) write a java stand alone retrieving data from database and put the data in XML format (i.e; we have to create an XML file).
    How to do this? Could any one of you please suggest me how to code or can i have any links that refer the code snippets.
    Thanks.

    Could someone give me a link with information on how SAX is used to create an XML?
    null

  • How to create two level dynamic list using JSP , Java Script and Oracle

    I am new in JSP. And i am facing problem in creating two level dynamic list using JSP ,Java Script where the listdata will come from Oracle 10g express edition database. Is there any easy way in JSP that is available on in ASP.NET.
    Plz response with details.

    1) Learn JDBC API [http://java.sun.com/docs/books/tutorial/jdbc/index.html].
    2) Create DAO class which contains JDBC code and do all SQL queries and returns or takes ID's or DTO objects.
    3) Learn Servlet API [http://java.sun.com/javaee/5/docs/tutorial/doc/].
    4) Create Servlet class which calls the DAO class, gets the list of DTO's as result, puts it as a request attribute and forwards the request to a JSP page.
    5) Learn JSP and JSTL [http://java.sun.com/javaee/5/docs/tutorial/doc/]. Also learn HTML if you even don't know it.
    6) Create JSP page which uses the JSTL c:forEach tag to access the list of DTO's and iterate over it and prints a HTML list out.
    You don't need Javascript for this.

  • Using jsp creating the xml file on the browser

    Hi All,
    My problem is i have jsp page by which i am getting the data from data base, by using the database table i want to create the xml file
    can anybody help in this issue

    Oh, "the thing". Let me see if I can find that code for you ...

  • How I can create a XML file from java Aplication

    How I can create a XML file from java Aplication
    whith have a the following structure
    <users>
    <user>
    <login>anyName</login>     
    <password>xxxx</password>
    </user>
    </users>
    the password label must be encripted
    accept any suggestion

    Let us assume you have all the data from the jsp form in an java bean object..
    Now you want a xml file. This can be acheived in 2 ways
    1. Write it into a file using java.io classes. Say you have a class with name
    write("<name>"+obj.getName+</name>);
    bingo you have a flat file with the xml
    2. Use data binding to do the trick
    will recommend JiBx and Castor for the 2nd option
    Regards,
    Rajagopal

  • Creating an xml file from java.

    I trying to create an xml file using a java program. I just wondering what is the best way to go about it and what should i use jdom ,xerces sax etc.

    Use JAXP+SAX.
    Here an example:import java.io.*;
    // SAX classes.
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    //JAXP 1.1
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.*;
    import javax.xml.transform.sax.*;
    // PrintWriter from a Servlet
    PrintWriter out = response.getWriter();
    StreamResult streamResult = new StreamResult(out);
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    // SAX2.0 ContentHandler.
    TransformerHandler hd = tf.newTransformerHandler();
    Transformer serializer = hd.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT,"yes");
    hd.setResult(streamResult);
    hd.startDocument();
    AttributesImpl atts = new AttributesImpl();
    // USERS tag.
    hd.startElement("","","USERS",atts);
    // USER tags.
    String[] id = {"PWD122","MX787","A4Q45"};
    String[] type = {"customer","manager","employee"};
    String[] desc = {"Tim@Home","Jack&Moud","John D'o�"};
    for (int i=0;i<id.length;i++)
      atts.clear();
      atts.addAttribute("","","ID","CDATA",id);
    atts.addAttribute("","","TYPE","CDATA",type[i]);
    hd.startElement("","","USER",atts);
    hd.characters(desc[i].toCharArray(),0,desc[i].length());
    hd.endElement("","","USER");
    hd.endElement("","","USERS");
    hd.endDocument();
    [i]This xml generation program might be the best solution because it uses JAXP 1.1 so it will work under JDK 1.4 or JDK 1.2/1.3 with XALAN2 library (or any XML library JAXP 1.1 compliant). It's also memory-friendly because it doesn't need DOM.
    See http://www.javazoom.net/services/newsletter/xmlgeneration.html
    Hope That Helps                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Create an XML file from Java

    I'm a new user of XML. I've a very important question:
    Is it possible create an XML file using some java package? If yes what package i must use? JDOM is the right product?
    Thanks

    Yes, JDOM is the right choice for 99% of your work.
    DOM is too difficult to use.
    That is the reason why JDOM has been developped.

  • Creating an xml file from the Basic java Object

    how to create an XML file using the values available in the object with reference to an xsd or dtd file..
    (OR )
    is it possible to write the contents of an object to an xml file without knowing the dtd or xsd file .......

    how to create an XML file using the values available in the object with reference to an xsd or dtd file..
    (OR )
    is it possible to write the contents of an object to an xml file without knowing the dtd or xsd file .......

  • How to create new XML file using retreived XML content by using SAX API?

    hi all,
    * How to create new XML file using retreived XML content by using SAX ?
    * I have tried my level best, but output is coming invalid format, my code is follows,
    XMLFileParser.java class :-
    import java.io.StringReader;
    import java.io.StringWriter;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMResult;
    import javax.xml.transform.sax.SAXSource;
    import javax.xml.transform.sax.SAXTransformerFactory;
    import javax.xml.transform.sax.TransformerHandler;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.XMLFilterImpl;
    public class PdfParser extends XMLFilterImpl {
        private TransformerHandler handler;
        Document meta_data;
        private StringWriter meta_data_text = new StringWriter();
        public void startDocument() throws SAXException {
        void startValidation() throws SAXException {
            StreamResult streamResult = new StreamResult(meta_data_text);
            SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            try
                handler = factory.newTransformerHandler();
                Transformer transformer = handler.getTransformer();
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                handler.setResult(streamResult);
                handler.startDocument();
            catch (TransformerConfigurationException tce)
                System.out.println("Error during the parse :"+ tce.getMessageAndLocation());
            super.startDocument();
        public void startElement(String namespaceURI, String localName,
                String qualifiedName, Attributes atts) throws SAXException {
            handler.startElement(namespaceURI, localName, qualifiedName, atts);
            super.startElement(namespaceURI, localName, qualifiedName, atts);
        public void characters(char[] text, int start, int length)
                throws SAXException {
            handler.characters(text, start, length);
            super.characters(text, start, length);
        public void endElement(String namespaceURI, String localName,
                String qualifiedName) throws SAXException {
            super.endElement("", localName, qualifiedName);
            handler.endElement("", localName, qualifiedName);
        public void endDocument() throws SAXException {
        void endValidation() throws SAXException {
            handler.endDocument();
            try {
                TransformerFactory transfactory = TransformerFactory.newInstance();
                Transformer trans = transfactory.newTransformer();
                SAXSource sax_source = new SAXSource(new InputSource(new StringReader(meta_data_text.toString())));
                DOMResult dom_result = new DOMResult();
                trans.transform(sax_source, dom_result);
                meta_data = (Document) dom_result.getNode();
                System.out.println(meta_data_text);
            catch (TransformerConfigurationException tce) {
                System.out.println("Error occurs during the parse :"+ tce.getMessageAndLocation());
            catch (TransformerException te) {
                System.out.println("Error in result transformation :"+ te.getMessageAndLocation());
    } CreateXMLFile.java class :-
    Sax.startDocument();
    Sax.startValidation();
    Sax.startElement("", "pdf", "pdf", new AttributesImpl());
    Sax.startElement("", "basic-metadata", "basic-metadata", new AttributesImpl());          
    String xmp_str = new String(meta_data.getByteArray(),"UTF8");
    char[] xmp_arr = xmp_str.toCharArray();
    Sax.characters(xmp_arr, 0, xmp_arr.length);
    Sax.endElement("", "pdf", "pdf");
    Sax.endValidation();
    Sax.endDocument();
    Sax.endElement("", "basic-metadata", "basic-metadata");* In CreateXMLFile.java
    class, I have retreived the xml content in the meta_data object, after that i have converted into character array and this will be sends to SAX
    * In this case , the XML file created successfully but the retreived XML content added as an text in between basic-metadata Element, that is, retreived XML content
    is not an XML type text, it just an Normal text Why that ?
    * Please help me what is the problem in my code?
    Cheers,
    JavaImran

    Sax.startDocument();
    Sax.startValidation();
    Sax.startElement("", "pdf", "pdf", new AttributesImpl());
    Sax.startElement("", "basic-metadata", "basic-metadata", new AttributesImpl());          
    String xmp_str = new String(meta_data.getByteArray(),"UTF8");
    char[] xmp_arr = xmp_str.toCharArray();
    Sax.characters(xmp_arr, 0, xmp_arr.length);
    </code><code>Sax.endElement("", "basic-metadata", "basic-metadata");</code>
    <code class="jive-code jive-java">Sax.endElement("", "pdf", "pdf");
    Sax.endValidation();
    Sax.endDocument();     
    * I HAVE CHANGED MY AS PER YOUR SUGGESTION, NOW SAME RESULT HAS COMING.
    * I AM NOT ABLE TO GET THE EXACT OUTPUT.,WHY THAT ?
    Thanks,
    JavaImran{code}

  • How to create a xml file from the jsp page?

    I'm a beginner of the develop xml,my question like this,
    there has a jsp page,some parameters in it,I wanna get the parameters and create a xml file,so,what parser method should I use?
    And how to create a new xml file,when the file haven't exist at first?
    pls give some code,thanks.

    a ggod link for u http://www.theserverside.com/resources/article.jsp?l=JSP-XML2

  • How do i create and write to an xml file?

    I know there are methods to create nodes and such but I am not at all familiar with xml. Isn't an easier way just to use the File class to create the file and use the write method to write to the xml file? Thank you.

    tsith wrote:
    <replies>
    <reply>
    <sentence>
    <pronoun>It</pronoun>
    <verb>is</verb>
    </sentence>
    </reply>
    </replies>
    Piddle! Try this: open up Microsoft Word, write "Hello, world!" and save it as XML:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <?mso-application progid="Word.Document"?>
    <w:wordDocument
        xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
        xmlns:v="urn:schemas-microsoft-com:vml"
        xmlns:w10="urn:schemas-microsoft-com:office:word"
        xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
        xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
        xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
        xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
        xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
        w:macrosPresent="no"
        w:embeddedObjPresent="no"
        w:ocxPresent="no"
        xml:space="preserve">
        <w:ignoreElements w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
        <o:DocumentProperties>
            <o:Title>Hello, world</o:Title>
            <o:Author>BDLH</o:Author>
            <o:LastAuthor>BDLH</o:LastAuthor>
            <o:Revision>1</o:Revision>
            <o:TotalTime>0</o:TotalTime>
            <o:Created>2008-09-19T17:37:00Z</o:Created>
            <o:LastSaved>2008-09-19T17:37:00Z</o:LastSaved>
            <o:Pages>1</o:Pages>
            <o:Words>2</o:Words>
            <o:Characters>12</o:Characters>
            <o:Company>Weight Watchers</o:Company>
            <o:Lines>1</o:Lines>
            <o:Paragraphs>1</o:Paragraphs>
            <o:CharactersWithSpaces>13</o:CharactersWithSpaces>
            <o:Version>11.0000</o:Version>
        </o:DocumentProperties>
        <w:fonts>
            <w:defaultFonts w:ascii="Times New Roman" w:fareast="Times New Roman" w:h-ansi="Times New Roman" w:cs="Times New Roman"/>
        </w:fonts>
        <w:styles>
            <w:versionOfBuiltInStylenames w:val="4"/>
            <w:latentStyles w:defLockedState="off" w:latentStyleCount="156"/>
            <w:style w:type="paragraph" w:default="on" w:styleId="Normal">
                <w:name w:val="Normal"/>
                <w:rPr>
                    <wx:font wx:val="Times New Roman"/>
                    <w:sz w:val="24"/>
                    <w:sz-cs w:val="24"/>
                    <w:lang w:val="EN-US" w:fareast="EN-US" w:bidi="AR-SA"/>
                </w:rPr>
            </w:style>
            <w:style w:type="character" w:default="on" w:styleId="DefaultParagraphFont">
                <w:name w:val="Default Paragraph Font"/>
                <w:semiHidden/>
            </w:style>
            <w:style w:type="table" w:default="on" w:styleId="TableNormal">
                <w:name w:val="Normal Table"/><wx:uiName wx:val="Table Normal"/>
                <w:semiHidden/>
                <w:rPr>
                    <wx:font wx:val="Times New Roman"/>
                </w:rPr>
                <w:tblPr>
                    <w:tblInd w:w="0" w:type="dxa"/>
                    <w:tblCellMar>
                        <w:top w:w="0" w:type="dxa"/>
                        <w:left w:w="108" w:type="dxa"/>
                        <w:bottom w:w="0" w:type="dxa"/>
                        <w:right w:w="108" w:type="dxa"/>
                    </w:tblCellMar>
                </w:tblPr>
            </w:style>
            <w:style w:type="list" w:default="on" w:styleId="NoList">
                <w:name w:val="No List"/>
                <w:semiHidden/>
            </w:style>
        </w:styles>
        <w:docPr>
            <w:view w:val="print"/>
            <w:zoom w:percent="100"/>
            <w:doNotEmbedSystemFonts/>
            <w:proofState w:spelling="clean" w:grammar="clean"/>
            <w:attachedTemplate w:val=""/>
            <w:defaultTabStop w:val="720"/>
            <w:punctuationKerning/>
            <w:characterSpacingControl w:val="DontCompress"/>
            <w:optimizeForBrowser/>
            <w:validateAgainstSchema/>
            <w:saveInvalidXML w:val="off"/>
            <w:ignoreMixedContent w:val="off"/>
            <w:alwaysShowPlaceholderText w:val="off"/>
            <w:compat>
                <w:breakWrappedTables/>
                <w:snapToGridInCell/>
                <w:wrapTextWithPunct/>
                <w:useAsianBreakRules/>
                <w:dontGrowAutofit/>
            </w:compat>
            <wsp:rsids>
                <wsp:rsidRoot wsp:val="0077226E"/>
                <wsp:rsid wsp:val="0077226E"/>
            </wsp:rsids>
        </w:docPr>
        <w:body>
            <wx:sect>
                <w:p wsp:rsidR="0077226E" wsp:rsidRDefault="0077226E">
                    <w:r>
                        <w:t>Hello, world!</w:t>
                    </w:r>
                </w:p>
                <w:sectPr wsp:rsidR="0077226E">
                    <w:pgSz w:w="12240" w:h="15840"/>
                    <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="708" w:footer="708" w:gutter="0"/>
                    <w:cols w:space="708"/>
                    <w:docGrid w:line-pitch="360"/>
                </w:sectPr>
            </wx:sect>
        </w:body>
    </w:wordDocument>

  • Use of Java Code within the generated XML Forms Stylesheets

    Hello,
    is the use of Java-Code possible with the XSL-Files generated by XML-Forms, as possible in standard XSLs? I know that this would mean a modification.
    kind regards,
    Marco

    Hi Marco,
    I "executed" some java code within XSLs file. I have quoted the word executed because I didn't really run java code, but I used a simply trick that I describe you below.
    Into your XSLs put an iframe which is hided (it has an height of 0 pixel). As src of the iframe put the address of a portal component which execute your code (in my case calculate some PCD URL of some pages into a defined role). As result of component execution, I use the response.write method in order to execute some jscript code, which is able to interact with the HTML generated by XSLs files, for example in my case response.write put the PCD URL into a drop down list placed into the "edit" form.
    This works fine. I don't know if is suitable also for your case, anyway could be a hint, but pay attention to Roland's recommendations.
    Ciao
    Roberto

  • SAX: How to create new XML file using SAX parser

    Hi,
    Please anybody help me to create a XML file using the Packages in the 5.0 pack of java. I have successfully created it reading the tag names and values from database using DOM but can i do this using SAX.
    I am successful to read XML using SAX, now i want to create new XML file for some tags and its values using SAX.
    How can i do this ?
    Sachin Kulkarni

    SAX is a parser, not a generator.Well,
    you can use it to create an XML file too. And it will take care of proper encoding, thus being much superior to a normal textwriter:
    See the following code snippet (out is a OutputStream):
    PrintWriter pw = new PrintWriter(out);
          StreamResult streamResult = new StreamResult(pw);
          SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
          //      SAX2.0 ContentHandler.
          TransformerHandler hd = tf.newTransformerHandler();
          Transformer serializer = hd.getTransformer();
          serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//
          serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"pdfBookmarks.xsd");
          serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"http://schema.inplus.de/pdf/1.0");
          serializer.setOutputProperty(OutputKeys.METHOD,"xml");
          serializer.setOutputProperty(OutputKeys.INDENT, "yes");
          hd.setResult(streamResult);
          hd.startDocument();
          //Get a processing instruction
          hd.processingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"mystyle.xsl\"");
          AttributesImpl atts = new AttributesImpl();
          atts.addAttribute("", "", "someattribute", "CDATA", "test");
          atts.addAttribute("", "", "moreattributes", "CDATA", "test2");
           hd.startElement("", "", "MyTag", atts);
    String curTitle = "Something inside a tag";
              hd.characters(curTitle.toCharArray(), 0, curTitle.length());
        hd.endElement("", "", "MyTag");
          hd.endDocument();
    You are responsible for proper nesting. SAX takes care of encoding.
    Hth
    ;-) stw

Maybe you are looking for

  • ERROR while saving the runtime systems(read time out exception)

    Hi e xperts I am configuring NWDI and assigned asll the permissions and roles to the CMS user.I created Domain,in landscape configurater created track and saved. while saving the rumtime systems the error thrown is com.sap.cms.util.exception.conf.CMS

  • Which Itouch should I buy?

    This is my first post so please bare with me. I know next to nothing about the itouch and am trying to decide which one to buy for my wife. She has an old Sony Clie (Palm OS)she used for the calendar, contacts and storing medical programs but it just

  • Has anyone solved an Epson printing problem?

    After I upgraded to Leopard, I noticed that my printing on my Epson Stylus Photo R1800 was poor in quality to say the least. I tried all of the usual troubleshooting techniques and then emailed Epson. They directed me to a new driver, and they also s

  • Wait for user input in for loop

    I'm pretty new to labview and I'm having a hard time figuring out how to do this. Basically, I need the user to press a GO button and data aquisition will take place saving it in an array, then the program will wait for the user to press the GO butto

  • SOA Composite Editor Missing in 11.1.2 Or Not Yet Released?

    Hi, I downloaded the JDeveloper Studio 11.1.2 and there is no SOA composite editor (I saw the note in the page after the setup). The SOA is not included or not included yet? If it is not included some people know why? Thanks Fabio D'Alfonso