Skip to main content

jaxp validation problem

1 reply [Last post]
fbn79
Offline
Joined: 2008-06-03
Points: 0

Hi,
I'm using jaxp for parsing and validating (xml schema) a xml file.

This is what I do...
==================================

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating( true );
try{
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
} catch (Exception e) {
e.printStackTrace(System.err);
}

DocumentBuilder db = dbf.newDocumentBuilder();
OutputStreamWriter errorWriter =
new OutputStreamWriter(System.err, outputEncoding);

db.setErrorHandler(
new SimpleErrorHandler(
new PrintWriter(errorWriter, true)
)
);

try{

Document document =
db.parse( new File( "src/xml/sudoku01.xml" ));
}
catch( SAXException se ){
System.err.println(se.getMessage());
}

==================================

All works right, the xml file is loaded, parsed and validated right.

But now I need to parse an InputStream, not directly a File object.
But if I simply change the File object with a stream the parser stop validating telling me that the xml file don't have good elements.

Change applied to precedent code....
==================================
try{
InputStream ist = new FileInputStream(
new File("src/xml/sudoku01.xml")
);

Document document =
db.parse( ist );

}
==================================

Where is my fault in using InputStream rather than directly a File object?

Hope anyone can help me :)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
joehw
Offline
Joined: 2004-12-15
Points: 0

Can you provide runable testcase along with xml/schema files?

A suggestion to all posters: please always provide ready-to-run testcase (code and files). People are extremely busy and won't be able to read closely and spend time reproducing issues. It's my experience with colleagues too -- I might spend 10 min. trying to describe an issue, but at the end, I'd be asked: do you have a testcase? I'll look at it if you have a testcase :)

So please do, cause' you're the one who knows your case the best. It would save everyone's time. Most importantly, it helps getting your question answered timely.

Thanks,
JoeJoe