-
Convert Document object to String object (3 messages)
- Posted by: Matt Louden
- Posted on: May 21 2004 00:51 EDT
I want to parse the XML file, and output the string object of XML representation.
I tried to convert Document object to String object, but it is unsuccessful. Anyone knows
what I miss?
Here's the output of this program, which is wrong:
[person: null]
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
/** transform Document object to XML string */
public class ParserTest
{
public static void main(String[] args) throws ParserConfigurationException, SAXException
{
try
{
String xmlFile = "mydoc.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(xmlFile));
Element root = doc.getDocumentElement();
String s = root.toString();
System.out.println(s);
}
catch(IOException e)
{ e.printStackTrace();
}
}
}
//mydoc.xml
<?xml version = "1.0"?>
<person>
<name>Joe</name>
<age>20</age>
</person>Threaded Messages (3)
- Convert Document object to String object by Ravi Srirangam on May 21 2004 02:19 EDT
- Thanks Ravi Sri by Nagesh Babu S on August 30 2004 13:22 EDT
- Convert Document object to String object by vijay m on April 08 2011 08:33 EDT
-
Convert Document object to String object[ Go to top ]
- Posted by: Ravi Srirangam
- Posted on: May 21 2004 02:19 EDT
- in response to Matt Louden
Matt,
Here is the method that converts the Document to String equivalent
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
//method to convert Document to String
public String getStringFromDocument(Document doc)
{
try
{
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
catch(TransformerException ex)
{
ex.printStackTrace();
return null;
}
} -
Thanks Ravi Sri[ Go to top ]
- Posted by: Nagesh Babu S
- Posted on: August 30 2004 13:22 EDT
- in response to Ravi Srirangam
Hi Ravi Sri,
Thanks a ton for your smart code suggestion to Matt, this piece of code helped me to go home after a hectic java to xml convertion project.
Best regards
Nagesh
nageshbab@rediffmail.com -
Convert Document object to String object[ Go to top ]
- Posted by: vijay m
- Posted on: April 08 2011 08:33 EDT
- in response to Matt Louden
Hi,
It is good logic whatever you are using. made some chages to your code .now it is working fine. find the working code.
package com.test;
import java.io.File;
import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Sample {
public static void main(String[] args)
{
try
{
String xmlFile = "src/com/test/Sample.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(xmlFile));
Element root = doc.getDocumentElement();
String s = root.toString();
System.out.println(s);
}
catch(Exception e)
{ e.printStackTrace();
}
}
}