Get problem in loading the xml code

Hi all,

I have got one problem in program. </method >not get generates after method block.

I want output like this:

<?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>

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

--</methods>

-<methods>

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

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

</methods>

</interface>

But the output comes 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>

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

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

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

</methods>

</interface>

************************************************************

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"));

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

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

Node methodnode = packageDoc.getElementsByTagName("methods").item(0);

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

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

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");

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

// Create the document fragment node to hold the new nodes

DocumentFragment docfrag = packageDoc.createDocumentFragment();

// Move the nodes into the fragment

while (node.hasChildNodes())

{

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

}

methodnode.appendChild(docfrag);

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

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

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

// Create the document fragment node to hold the new nodes

docfrag = packageDoc.createDocumentFragment();

// Move the nodes into the fragment

while (node.hasChildNodes())

{

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

}

methodnode.appendChild(docfrag);

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)

{

}

}

}

*********************************************************************

Please help me.

Thanks

bunty

[5933 byte] By [bunty_bargea] at [2007-11-26 17:55:33]
# 1

Replace the following line:methodnode.appendChild(docfrag);

with these lines:Node methodnode2 = packageDoc.createElement("methods");

packageDoc.getDocumentElement().appendChild(methodnode2);

methodnode2.appendChild(docfrag);

The problem was that you omitted to create a new <methods> element for the document fragment to be inserted in, so it got inserted in the original <methods> element.

prgguya at 2007-7-9 5:08:43 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...