File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
wood burning stoves
subject: How to convert Nodelist to a String using JAVA
Similar Threads
help with web service to display xml file contents using netbeans
convert a String to Long
Strange behaviour with CachedXPathAPI
how to convert string to int
how to convert Byte to the String