validating XML against schema using JDOM

S

srujana

Hi

I am validating an xml document against schema .
when i am passing the xml file location as an input to the parse method

String XmlDocumentUrl="E:/books.xml";
parser.parse(XmlDocumentUrl);

it is working fine.

but how can i do that if i need to pass a xml document content instead
of the file location.

String XmlDocumentUrl="E:/books.xml";
InputSource inputSource = new InputSource(new
ByteArrayInputStream(XmlDocumentUrl.getBytes()));
parser.parse(inputSource);

The error is Cannot find the declaration of element 'Books'
'Books' is my root element.

Any help is greatly appreciated.
Thanks.
 
J

Johannes Koch

srujana said:
String XmlDocumentUrl="E:/books.xml";
InputSource inputSource = new InputSource(new
ByteArrayInputStream(XmlDocumentUrl.getBytes()));

This will create an input stream from the bytes in the string
"E:/books.xml", not from the bytes in the file referenced by "E:/books.xml".
 
S

Soren Kuula

srujana said:
Hi

I am validating an xml document against schema .
when i am passing the xml file location as an input to the parse method

String XmlDocumentUrl="E:/books.xml";
parser.parse(XmlDocumentUrl);

it is working fine.

but how can i do that if i need to pass a xml document content instead
of the file location.

String XmlDocumentUrl="E:/books.xml";
InputSource inputSource = new InputSource(new
ByteArrayInputStream(XmlDocumentUrl.getBytes()));
parser.parse(inputSource);

The error is Cannot find the declaration of element 'Books'
'Books' is my root element.

It could be something with base URI?

With your first method, the parser has a chance to resolve relative URIs
in your document, in order to load other resources, such as the schema.
If the document contains this schema-location element thing (can't
remember its name) with schema location "foo.xsd", the parser will
resolve that to e:/foo.xsd . With your second method it doesn't have a
chance, because it never heard about e:/ .

You can probably set the base URI on the InputSource that you use,
letting the parser know the base URI. If you don't want to use
InputSources at all, you have to give the parser a schema location hint
by calling some method on it, or make your own entity resolver and have
to parser use that.

Soren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top