Converting Node to a String
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Prasad_Potluri
Posted On:   Thursday, December 13, 2001 05:29 PM

Hi,

How can I convert a Node into a String?

My XML file looks like:



1
ABC


2
DEF



I wanted to assign student nodes to a String Array like

String students[];

students [0]= " 1 ABC "

Any ideas how to do this??

thanx in adv.

Re: Converting Node to a String

Posted By:   Steven_Devijver  
Posted On:   Sunday, January 6, 2002 07:55 AM

To convert a node and its children to a string with Xerces you need to use the XMLSerializer class:


// create parser and load XML document.
// retrieve the root element.
Element xmlElement = xmldoc.getDocumentElement();

// initialize Writer class.
StringWriter xmlOut = new StringWriter();
// initialize XMLSerializer class.
XMLSerializer xmlser = new XMLSerializer(xmlOut, new OutputFormat(xmldoc));

// mandatory to catch IOException.
try {
// serialize the Element.
xmlser.serialize(xmlElement);
} catch (IOException e) {
e.printStackTrace();
}

// et voila.
String xmlString = xmlOut.toString();


You may want to read the API doc for Xerces to get to know the XMLSerializer class.
About | Sitemap | Contact