Prasanna23
asked on
How to convert Document object to Input Source ?
Hi all,
I am having a XML document objecy created on the fly. I need to validate it against Schema. I am using xerces 1.4.0. I have set features for the parser.Now i need to parse to validate the XML.
For this i need to call "parser.parse()". But parse() method takes "InputSource" as parameter. But i have Document object. How do i convert this Document object to "InputSource" for passing it to parse() method.
Can anybody help.
Best Regards,
Prasanna
I am having a XML document objecy created on the fly. I need to validate it against Schema. I am using xerces 1.4.0. I have set features for the parser.Now i need to parse to validate the XML.
For this i need to call "parser.parse()". But parse() method takes "InputSource" as parameter. But i have Document object. How do i convert this Document object to "InputSource" for passing it to parse() method.
Can anybody help.
Best Regards,
Prasanna
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi ozymandias,
I tried the above method. Technically it looked fine. But when i tried to print the String got using e.toString() it just printed "[response:null]". "response" is my root element. I checked up my Document object it is not null.
Will Element.toString() work directly. I am toying over idea of XMLSerializer to get String. I have a slight doubt about Element.toString(). Please confirm about Element.toString() or is there any other method to convert it to String.
Thanks and Best Regards,
Prasanna
I tried the above method. Technically it looked fine. But when i tried to print the String got using e.toString() it just printed "[response:null]". "response" is my root element. I checked up my Document object it is not null.
Will Element.toString() work directly. I am toying over idea of XMLSerializer to get String. I have a slight doubt about Element.toString(). Please confirm about Element.toString() or is there any other method to convert it to String.
Thanks and Best Regards,
Prasanna
ASKER
Hi,
Thanks to both of u. But i dont know whose answer to accept. Both are correct. I am in a fix. I couldnt find provision to accept both answers. What do i do?????
Thanks and Best Regards,
Prasanna
Thanks to both of u. But i dont know whose answer to accept. Both are correct. I am in a fix. I couldnt find provision to accept both answers. What do i do?????
Thanks and Best Regards,
Prasanna
ASKER
Thanks WMB & Ozymandias
Best Regards,
Prasanna
Best Regards,
Prasanna
Prasanna23,
Element.toString() will not return the XML representation of the Element. To produce XML, you need to know the output type and output format (encoding), and XMLSerializer provides that functionality.
Here's an example:
import java.io.*;
import org.apache.xerces.parsers. *;
import org.apache.xml.serialize.* ;
import org.w3c.dom.*;
import org.xml.sax.*;
public class ElemString
{
public static void main(String[] args)
{
try
{
DOMParser parser = new DOMParser();
parser.parse(args[0]);
Document doc = parser.getDocument();
System.out.println("Trying toString()...");
Element elem = doc.getDocumentElement();
String xml = elem.toString();
System.out.println(xml);
System.out.println("Doesn' t work!");
System.out.println("Trying serialize...");
StringWriter sw = new StringWriter();
XMLSerializer ser = new XMLSerializer(sw, new OutputFormat("xml", "UTF-8", true));
ser.serialize(doc);
xml = sw.toString();
System.out.println(xml);
System.out.println("Looks good!");
parser.parse(new InputSource(new StringReader(xml)));
System.out.println("Parsed OK!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Regards,
WMB
Element.toString() will not return the XML representation of the Element. To produce XML, you need to know the output type and output format (encoding), and XMLSerializer provides that functionality.
Here's an example:
import java.io.*;
import org.apache.xerces.parsers.
import org.apache.xml.serialize.*
import org.w3c.dom.*;
import org.xml.sax.*;
public class ElemString
{
public static void main(String[] args)
{
try
{
DOMParser parser = new DOMParser();
parser.parse(args[0]);
Document doc = parser.getDocument();
System.out.println("Trying
Element elem = doc.getDocumentElement();
String xml = elem.toString();
System.out.println(xml);
System.out.println("Doesn'
System.out.println("Trying
StringWriter sw = new StringWriter();
XMLSerializer ser = new XMLSerializer(sw, new OutputFormat("xml", "UTF-8", true));
ser.serialize(doc);
xml = sw.toString();
System.out.println(xml);
System.out.println("Looks good!");
parser.parse(new InputSource(new StringReader(xml)));
System.out.println("Parsed
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Regards,
WMB
ASKER
Thanks wmb,
I have done the same thing. I have used setOutputStream instead.
Thanks and Best Regards,
Prasanna
I have done the same thing. I have used setOutputStream instead.
Thanks and Best Regards,
Prasanna
You need to serialize the Document to a string (or maybe a file) using XMLSerializer(StringWriter
Regards,
WMB