wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes How to convert  Nodelist to a String using JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to convert  Nodelist to a String using JAVA" Watch "How to convert  Nodelist to a String using JAVA" New topic
Author

How to convert Nodelist to a String using JAVA

Krysty Sullivan
Ranch Hand

Joined: Jun 06, 2002
Posts: 48
How do I convert a NodeList to its corresponding XML String using JAVA ?
[ August 19, 2002: Message edited by: Krysty Sullivan ]
Selin Ebeci
Greenhorn

Joined: Dec 17, 2009
Posts: 5
Hi,

You can only convert a Node object to an XML string due to the fact that the root element must be well-formed. Anyway you can convert NodeList object to a Node easily, check this out... :

/* To read xml string*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(is);

/* Create a sub xml starts with given tag name soaphead*/
NodeList soaphead = doc.getElementsByTagName("soaphead");
StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(soaphead.item(0)), new StreamResult(sw));
String result = sw.toString();

Cheers,
Selin Ebeci
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert Nodelist to a String using JAVA