aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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

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

Joined: Dec 17, 2009
Posts: 5

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

Selin Ebeci
I agree. Here's the link:
subject: How to convert Nodelist to a String using JAVA
Similar Threads
Strange behaviour with CachedXPathAPI
convert a String to Long
how to convert Byte to the String
how to convert string to int
help with web service to display xml file contents using netbeans