Solved

writing values into a XML file from a JSP

Posted on 2003-11-18
5
518 Views
Last Modified: 2008-01-16
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
 
0
Comment
Question by:GPR27
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:PeterCiuffetti
ID: 9779364
Hi,

I posted an answer to a previous question that covered the same topic here: http://www.experts-exchange.com/Web/Web_Languages/XML/Q_11806358.html
0
 
LVL 6

Accepted Solution

by:
PeterCiuffetti earned 90 total points
ID: 9779672
I think I may have misunderstood the original question.  I see now you want to add XML fragmests where the input comes from a form processed by JSP.  Assuming you have your original XML in a DOM, you add elements to the DOM this way:

        Element company = dom.getDocumentElement();
        Element employee = dom.createElement( "Employee" );
        Element employeeDetails = dom.createElement( "EmployeeDetails" );

        if ( request.getParameter("name") != null ) {
                name_text = request.getParameter("name");
                Element name = dom.createElement( "name" );
                Text text = dom.createTextNode( name_text );
                name.appendChild( text );
                employeeDetails.appendChild( name );
        }

        if ( request.getParameter("code") != null ) {
                code_text = request.getParameter("code");
                Element code = dom.createElement( "code" );
                Text text = dom.createTextNode( code_text );
                code.appendChild( text );
                employeeDetails.appendChild( code );
        }

        if ( request.getParameter("designation") != null ) {
                designation_text =  request.getParameter("designation");
                Element designation = dom.createElement( "Designation" );
                Text text = dom.createTextNode( designation_text );
                designation.appendChild( text );
                employeeDetails.appendChild( designation );
        }

        if ( request.getParameter("place") != null ) {
                place_text =  request.getParameter("place");
                Element place = dom.createElement( "Place" );
                Text text = dom.createTextNode( place_text );
                place.appendChild( text );
                employeeDetails.appendChild( place );
        }

        employee.appendChild( employeeDetails );
        company.appendChild( employee );
0
 

Author Comment

by:GPR27
ID: 9784720
Hi  PeterCiuffetti

Your comment is of  a great help to me.

Thanx a lot.

Regards
GPR27
0
 

Author Comment

by:GPR27
ID: 9785111
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
0
 
LVL 6

Expert Comment

by:PeterCiuffetti
ID: 9787357
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 );
    }


0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XSL Grouping 7 42
Path of Workbook 3 77
Excel file not created as expected 7 72
Shredding xml into an oracle 11g Database 2 44
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question