1

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,

Ozer
  • 1,245
  • 4
  • 20
  • 27

3 Answers3

1
ByteArrayOutputStream docOutputStream = new ByteArrayOutputStream();
((XmlDocument)domDocument).write(docOutputStream);
ByteArrayInputStream docInputStream = new
ByteArrayInputStream(docOutputStream.toByteArray());
InputSource inputSource = new InputSource(docInputStream);
parser.parse(inputSource);
Jagat
  • 1,392
  • 2
  • 15
  • 25
0

See this question to convert the Document to an InputStream: how to create an InputStream from a Document or Node

Then use InputSource(java.io.InputStream byteStream) to wrap that with an InputSource.

Community
  • 1
  • 1
Jon7
  • 7,165
  • 2
  • 33
  • 39
0

You should be able to to this:

  • Create a javax.xml.validation.Schema instance based on your schema resources.
  • Create a javax.xml.validation.Validator from the schema instance
  • validate your DOM document using the validator and a javax.xml.transfrom.dom.DOMSource
forty-two
  • 12,204
  • 2
  • 26
  • 36