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

