Link to home
Start Free TrialLog in
Avatar of GPR27
GPR27

asked on

writing values into a XML file from a JSP

Hi

Well i am just new to XML . So, i just need some help.
I have a XML file with a sample structure as follows :

<?xml version="1.0" encoding="UTF-8"?>
<Company>
      <Employee>
            <EmployeeDetails>      
                  <name>A</name>
                  <code>001</code>
                  <Designation>SET</Designation>
                  <place>PLACE1</place>
            </EmployeeDetails>      
            <EmployeeDetails>      
                  <name>B</name>
                  <code>002</code>
                  <Designation>SE</Designation>
                  <place>PLACE2</place>
            </EmployeeDetails>            
      </Employee>
</Company>

i am able to retrieve these values to a JSP and Display them in order.

Now i've a a few text fields to enter new data in the JSP .
How do i Enter these values entered in the JSP to the XML file in the same structure. Can anyone give me some idea regarding this. Iam using the APIs in org.xml.*, org.w3c.*, javax.xml.parsers.*.

Thanx in advance
Regards
GPR27
 
Avatar of PeterCiuffetti
PeterCiuffetti
Flag of United States of America image

Hi,

I posted an answer to a previous question that covered the same topic here: https://www.experts-exchange.com/questions/11806358/Passing-parameter-to-stylesheet-from-servlet.html
ASKER CERTIFIED SOLUTION
Avatar of PeterCiuffetti
PeterCiuffetti
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GPR27
GPR27

ASKER

Hi  PeterCiuffetti

Your comment is of  a great help to me.

Thanx a lot.

Regards
GPR27
Avatar of GPR27

ASKER

Hi

The idea suggested to write into a  XML file, is not writing into the XML file ??? i guess, it is getting lost in the dom object.

My intention is to get the data written into the XML file.. currently i open th exml file using the DocumantBuilderFactory with "parse" function, is this correct ???  OR do i need to open the XML File in the write mode using some other API ???

Please suggest

Regards
GPR27
Yes, the DOM is a memory object.  To write it out after modifying it, you have to use a serializer.  See http://xml.apache.org/xerces-j/apiDocs/org/apache/xml/serialize/XMLSerializer.html

import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.apache.xml.serialize.OutputFormat;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

/**
 * Utility class to serialize a DOM to an OutputStream
 */
public class DOMSerializer
{
    /**
     * Serializer the supplied DOM
     * @param Document - the DOM to serialize
     * @param OutputStream - stream to serialize to
     * @param boolean - true if XML entities are escaped
     * @param String[] - elements in which not to escape entities null if escapeEntities is true
     * @param boolean - true if pretty print in, use only if going to file
     */
    public static void serialize( Document dom,
                                  OutputStream output,
                                  boolean escapeEntities,
                                  String[] nonEscapingElements,
                                  boolean prettyPrint ) throws IOException
    {
        // Set up output format depending on options
        OutputFormat format = new OutputFormat( dom, null, prettyPrint );
        // Always preserve whitespace
        format.setPreserveSpace( true );
        if( escapeEntities )
        {
            format.setNonEscapingElements( nonEscapingElements );
        }

        // Now create a serializer, give it the output
        XMLSerializer serializer = new XMLSerializer( format );
        serializer.setOutputCharStream( new OutputStreamWriter(output, "UTF-8"));
        // Do it
        serializer.serialize( dom );
    }