Search code examples
javaxmlxercesjdom

How to convert Document object to Input Source?


I am having a XML document objecy created on the fly. I need to validate it against Schema. I am using xerces 2. 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,


Solution

  • ByteArrayOutputStream docOutputStream = new ByteArrayOutputStream();
    ((XmlDocument)domDocument).write(docOutputStream);
    ByteArrayInputStream docInputStream = new
    ByteArrayInputStream(docOutputStream.toByteArray());
    InputSource inputSource = new InputSource(docInputStream);
    parser.parse(inputSource);