got problem in loading xml

Hi All,

Because of your help I am doing small application and I am new in xml I gets small small problem and by the your help i cam solve it.

Now i have last problem when i solve it then my tool gets complete. I have attached small code of it and its working. The problem is when i fill one methods parameter information its comes in all methods. Which is wrong.

I want output like:

<?xml version="1.0" encoding="UTF-8"?>

<interface>

<name>the name of package</name>

<className>The name of class</className>

<methods>

<name>the name of the method</name>

<invokeKind/>

<returnType>thereturn type of the method</returnType>

<optionalParameters/>

<parameters>

<name>Param1</name>

<type>Void</type>

</parameters>

</methods>

<methods>

<name>the name of the method22</name>

<returnType>thereturn type of the method22</returnType>

<parameters/>

</methods>

</interface>

But output comes like

<?xml version="1.0" encoding="UTF-8"?>

[code]<interface>

<name>the name of package</name>

<className>The name of class</className>

<methods>

<name>the name of the method</name>

<invokeKind/>

<returnType>thereturn type of the method</returnType>

<optionalParameters/>

<parameters>

<name>Param1</name>

<type>Void</type>

</parameters>

</methods>

<methods>

<name>the name of the method22</name>

<returnType>thereturn type of the method22</returnType>

<parameters>

<name>Param1</name>

<type>Void</type>

</parameters>

</methods>

</interface>

I have not given parameter in second method but given in first method but replicates in second thats y i am stucked,

Please help me.

Code is ::

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Document;

import org.w3c.dom.DocumentFragment;

import org.w3c.dom.Node;

publicclass TryXML

{

publicstaticvoid main(String args[])

{

try

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder parser = factory.newDocumentBuilder();

Document methoddoc = parser.parse(new File("methodinfo.xml"));

Document packageDoc = parser.parse(new File("interface.xml"));

//***Package information

Node packagenameNode = packageDoc.getElementsByTagName("name").item(0);

Node packageclassNode = packageDoc.getElementsByTagName("className").item(0);

packagenameNode.setTextContent("the name of package");

packageclassNode.setTextContent("The name of class");

//***First method information

Node nameNode = methoddoc.getElementsByTagName("name").item(0);

Node returnTypeNode = methoddoc.getElementsByTagName("returnType").item(0);

nameNode.setTextContent("the name of the method");

returnTypeNode.setTextContent("the return type of the method");

//**Parameter Informtion of this method.

Document parameterDoc = parser.parse(new File("parameter.xml"));

Node paramnameNode = parameterDoc.getElementsByTagName("name").item(0);

Node paramreturnTypeNode = parameterDoc.getElementsByTagName("type").item(0);

paramnameNode.setTextContent("Param1");

paramreturnTypeNode.setTextContent("Void");

//***adding parameter information into method.

Node methodaddnode = methoddoc.importNode(parameterDoc.getDocumentElement(),true);

DocumentFragment docfrag = methoddoc.createDocumentFragment();

//***Move the nodes into the fragment

while (methodaddnode.hasChildNodes())

{

docfrag.appendChild(methodaddnode.removeChild(methodaddnode.getFirstChild()));

}

Node paramNode = methoddoc.createElement("parameters");

methoddoc.getDocumentElement().appendChild(paramNode);

paramNode.appendChild(docfrag);

//***adding first methode's information into package.

Node node = packageDoc.importNode(methoddoc.getDocumentElement(),true);

docfrag = packageDoc.createDocumentFragment();

while (node.hasChildNodes())

{

docfrag.appendChild(node.removeChild(node.getFirstChild()));

}

Node methodnode = packageDoc.createElement("methods");

packageDoc.getDocumentElement().appendChild(methodnode);

methodnode.appendChild(docfrag);

//***adding second methode's information into package.

nameNode.setTextContent("the name of the method22");

returnTypeNode.setTextContent("the return type of the method22");

node = packageDoc.importNode(methoddoc.getDocumentElement(),true);

docfrag = packageDoc.createDocumentFragment();

while (node.hasChildNodes())

{

docfrag.appendChild(node.removeChild(node.getFirstChild()));

}

Node methodnode2 = packageDoc.createElement("methods");

packageDoc.getDocumentElement().appendChild(methodnode2);

methodnode2.appendChild(docfrag);

//**Display

DOMSource source =new DOMSource(packageDoc);

StreamResult result =new StreamResult(System.out);

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

System.out.println("This is the content of the XML document:\n");

transformer.transform(source, result);

}

catch (Exception e)

{

}

}

}

regards

-bunty

[8548 byte] By [bunty_bargea] at [2007-11-26 18:00:42]
# 1

The problem arises from the concatenation of two facts:

a) You use one and the same "methoddoc" document for creating both <methods> child elements of the <interface> element.

b) You have omitted to refresh the content of the child elements of the <parameters> element before adding them to the second <methods> child element of the <interface> element.

In short, you have only partially refreshed the content of the "methoddoc" document. I suggest following these steps:

1. Declare the "paramNode" node as an Element rather than a Node so that it can invoke the getElementsByTagName() method which makes your code more readable and manageable compared with using the methods of the Node interface:org.w3c.dom.Element paramNode = methoddoc.createElement("parameters");

2. Complete your code with two additional lines (the ones invoking the getElementsByTagName() method) to set the content of the child elements of the <parameters> element to the name and return type of the second method:...

returnTypeNode.setTextContent("the return type of the method22");

paramNode.getElementsByTagName("name").item(0).setTextContent("new name");

paramNode.getElementsByTagName("type").item(0).setTextContent("new type");

node = packageDoc.importNode(methoddoc.getDocumentElement(), true);

...

prgguya at 2007-7-9 5:30:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

my problem was, if i dont have parameters for second method then also parameters of first methods comes into 2nd method. I think its headache for me.

Parmeter.xml:

<parameter>

<name></name>

<type></type>

</parameter>

Method.xml

<method>

<name></name>

<returnType></returnType>

<parameters>

</parameters>

</method>

So my quetion is, I have put whole parameter.xml into method.xml's parameter node. In previous post u said refresh it. How can i refresh it ? not get this point .

Code is :

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class TryXML

{

public static void main(String args[])

{

try

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder parser = factory.newDocumentBuilder();

Document methoddoc = parser.parse(new File("method.xml"));

Document packageDoc = parser.parse(new File("interface.xml"));

Document parmerterDoc=parser.parse(new File("parameter.xml"));

//***Package information

Node packagenameNode = packageDoc.getElementsByTagName("name").item(0);

Node packageclassNode = packageDoc.getElementsByTagName("className").item(0);

packagenameNode.setTextContent("package1");

packageclassNode.setTextContent("class1");

//***First method information

Node nameNode = methoddoc.getElementsByTagName("name").item(0);

Node returnTypeNode = methoddoc.getElementsByTagName("returnType").item(0);

nameNode.setTextContent("method1");

returnTypeNode.setTextContent("int");

//**Parameter Informtion of this method.

Node paramnameNode = parmerterDoc.getElementsByTagName("name").item(0);

Node paramreturnTypeNode = parmerterDoc.getElementsByTagName("type").item(0);

paramnameNode.setTextContent("Param1");

paramreturnTypeNode.setTextContent("Void");

NodeList list = parmerterDoc.getElementsByTagName("parameter");

Element element = (Element)list.item(0);

NodeList list1 = methoddoc.getElementsByTagName("parameters");

Element element1 = (Element)list1.item(0);

// Make a copy of the element subtree suitable for inserting into doc2

Node dup = methoddoc.importNode(element, true);

// Insert the copy into doc2

element1.appendChild(dup);

NodeList methodlist = methoddoc.getElementsByTagName("method");

Element methodelement = (Element)methodlist.item(0);

NodeList methodlist1 = packageDoc.getElementsByTagName("methods");

Element methodelement1 = (Element)methodlist1.item(0);

// Make a copy of the element subtree suitable for inserting into doc2

Node methoddup = packageDoc.importNode(methodelement, true);

// Insert the copy into doc2

methodelement1.appendChild(methoddup);

//***Second method information

nameNode = methoddoc.getElementsByTagName("name").item(0);

returnTypeNode = methoddoc.getElementsByTagName("returnType").item(0);

nameNode.setTextContent("method2");

returnTypeNode.setTextContent("short");

//**Parameter Informtion of this method.

methodlist = methoddoc.getElementsByTagName("method");

methodelement = (Element)methodlist.item(0);

methodlist1 = packageDoc.getElementsByTagName("methods");

methodelement1 = (Element)methodlist1.item(0);

// Make a copy of the element subtree suitable for inserting into doc2

methoddup = packageDoc.importNode(methodelement, true);

// Insert the copy into doc2

methodelement1.appendChild(methoddup);

//**Display

DOMSource source = new DOMSource(packageDoc);

StreamResult result = new StreamResult(System.out);

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

System.out.println("This is the content of the XML document:\n");

transformer.transform(source, result);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

Message was edited by:

bunty_barge

bunty_bargea at 2007-7-9 5:30:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Soved the problem.Thanksbunty
bunty_bargea at 2007-7-9 5:30:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...