This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
 
subject: How to convert Nodelist to a String using JAVA
 
Similar Threads
help with web service to display xml file contents using netbeans
Strange behaviour with CachedXPathAPI
how to convert string to int
how to convert Byte to the String
convert a String to Long