• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xml problem

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have written one addServlet() which will add
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>First</servlet-class>
to the xml file.
similarly i have written addServletmapping() which will add
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
these both are working correctly
now if i want to add initparams
<init-param>
<param-name>aa</param-name>
<param-value>100</param-value>
</init-param>
we have to pass any number of name-value pairs
but when iam passing double dimensional arrays to addparam problem is comming.please tell me how to resolve this problem.Iam sending my code


import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class parse222
{
DocumentBuilderFactory factory = null;

DocumentBuilder builder = null;

Document document = null;
Element rootNode = null;

NodeList l = null;
Node refffNode = null;
Node reffNode = null;
Node refNode = null;
Element tempele4 = null;
Element tempele = null;
static Element tempele2 = null;
static Element tempele3 = null;
static Element tempele44 = null;
static Element tempelee2 = null;
static Element tempelee3 = null;
static String tempele4 = null;
static String tempele5 = null;
static String tempele6 = null;

static String[][] a = {
{
"Flintstones", "Fred", "Wilma",
"Pebbles", "Dino"}, {
"Rubbles", "Barney", "Betty",
"Bam Bam"}, {
"Jetsons", "George", "Jane",
"Elroy", "Judy", "Rosie", "Astro"}, {
"Scooby Doo Gang", "Scooby Doo",
"Shaggy", "Velma", "Fred", "Daphne"}
};


public void write() throws Exception {
factory = DocumentBuilderFactory.newInstance();

builder = factory.newDocumentBuilder();

document = builder.parse("d://web.xml");
rootNode = document.getDocumentElement();

NodeList l = rootNode.getChildNodes();
Node refffNode = l.item(1);
Node reffNode = l.item(1);
Node refNode = l.item(1);


}

public Element addServletMapping(Element a, Element b) throws Exception {

System.out.println("after creatomg xml file");

Element tempelee = document.createElement("servlet-mapping");

tempelee2 = document.createElement("servlet-name");
a = tempelee2;

tempelee3 = document.createElement("url-pattern");
b = tempelee3;

Text vall1 = document.createTextNode("initparam");

Text vall2 = document.createTextNode("/initparam.jsp");

Text vall3space = document.createTextNode("\n");

Text vall4space = document.createTextNode("\n");

Text vall5space = document.createTextNode("\n");

Text vall6space = document.createTextNode("\n");

a.appendChild(vall1);

b.appendChild(vall2);

tempelee.appendChild(vall3space);

tempelee.appendChild(a);

tempelee.appendChild(vall4space);

tempelee.appendChild(b);

tempelee.appendChild(vall5space);

rootNode.insertBefore( (Node) tempelee, reffNode);

rootNode.insertBefore( (Node) vall6space, reffNode);
return tempelee;

}

public int addParam(String[] a[i][j]) throws Exception {

tempele4 = document.createElement("init-param");
tempele5 = document.createElement("param-name");
tempele5 = a[0][0];
tempele6 = document.createElement("param-value");
tempele6 = a[0][1];

Text val3 = document.createTextNode("emailHost");
Text val4 = document.createTextNode("1000");
Text val3spacee = document.createTextNode("\n");

Text val4spacee = document.createTextNode("\n");

Text val5spacee = document.createTextNode("\n");

Text val6spacee = document.createTextNode("\n");

tempele5.appendChild(val3);
tempele6.appendChild(val4);
tempele4.appendChild(val3spacee);
tempele4.appendChild(tempele5);
tempele4.appendChild(val4spacee);
tempele4.appendChild(tempele6);
tempele4.appendChild(val5spacee);
tempele4.appendChild(val6spacee);
tempele.appendChild(tempele4);

rootNode.insertBefore( (Node) tempele4, refffNode);
rootNode.insertBefore( (Node) val6spacee, reffNode);
return 1;

}

public Element addServlet(Element t1, Element t2, Element t3) throws
Exception {

tempele = document.createElement("servlet");

tempele2 = document.createElement("servlet-name");
t1 = tempele2;

tempele3 = document.createElement("jsp-file");
t2 = tempele3;

tempele44 = document.createElement("servlet-class");
t3 = tempele44;
tempele4 = document.createElement("init-param");

Text val1 = document.createTextNode("sureshse");

Text val2 = document.createTextNode("initparam.jsp");
Text val33 = document.createTextNode("ParameterServlet");

Text val3space = document.createTextNode("\n");

Text val4space = document.createTextNode("\n");

Text val5space = document.createTextNode("\n");

Text val6space = document.createTextNode("\n");

t1.appendChild(val1);

t2.appendChild(val2);
t3.appendChild(val33);

tempele.appendChild(val3space);

tempele.appendChild(t1);

tempele.appendChild(val4space);

tempele.appendChild(t2);

tempele.appendChild(val5space);
tempele.appendChild(t3);
//tempele44.appendChild(val33);
//tempele.appendChild(tempele5);





rootNode.insertBefore( (Node) tempele, refNode);

rootNode.insertBefore( (Node) val6space, refNode);
return tempele;

}

public void writing() throws Exception {
TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(rootNode);
FileWriter fos = new FileWriter("d://web.xml");

StreamResult result = new StreamResult(fos);

transformer.transform(source, result);

}

public static void main(String args[]) throws Exception {
System.out.println("calling functions");
parse222 p = new parse222();
p.write();

p.addServlet(tempele2, tempele3, tempele44);
p.addParam(a[1][0]);
p.addServletMapping(tempelee2, tempelee3);

p.writing();

System.out.println(a[0][1] );

}
error is comming at addParam().please reply to this
}
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I see many other problems with your addParam method. But since you have specifically asked about passing two-dimensional array to this method, I will respond only on that.

First things first, Java and for that matter C or C++ don't allow passing of arrays in the manner you are trying to do, i,e:

addParams(String[] a[i][j])

If you are trying to pass the whole array, you need to do this

addParams(String[] a[][])

By the way, I see its a three-dimension array rather than a two-dimension as you mentioned.

For 2-D u need to do

addParam(String [][] a)

For 3-D u need to do

addParam(String[][][] a)

Refer some good Java programming guide and you would understand this easily.
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou .I got the solution
reply
    Bookmark Topic Watch Topic
  • New Topic