XML Validation

T

timothy.williams

Never done this....fairly new as well.

I am storing XML files on a server. Before I write the file to the
disk, I would like
to verify the syntax is correct. I don't want to validate it because I
don't have
any schema or data definition document to validate it against.

I just want to know the file is valid XML syntax.

How can I go about this?

thanks in advance.

TRW
 
S

Steve W. Jackson

Never done this....fairly new as well.

I am storing XML files on a server. Before I write the file to the
disk, I would like to verify the syntax is correct. I don't want to
validate it because I don't have any schema or data definition
document to validate it against.

I just want to know the file is valid XML syntax.

How can I go about this?

thanks in advance.

TRW

Given the description, I take "valid syntax" to mean that it's
well-formed XML. If so, why not simply attempt to parse it? If it
parses without error, it should be well-formed.

= Steve =
 
T

TRW

Thanks Steve.

Yes, I agree with that.

Is there a way to parse the document without having a definition
document
or schema?

Basically, just check pure syntax not to see if the tags are valid.
 
S

Steve W. Jackson

TRW said:
Thanks Steve.

Yes, I agree with that.

Is there a way to parse the document without having a definition
document or schema?

Basically, just check pure syntax not to see if the tags are valid.

Assuming you want to use the XML facilities built into Java, it's as
easy as pie. You'll need to call DocumentBuilderFactory.newInstance()
to get a new DocumentBuilderFactory, then configure factory (see the API
in javax.xml.parsers). Call the factory's newDocumentBuilder() method
to create a DocumentBuilder, and the call the builder's parse() method.
There are multiple parse methods, allowing you to parse a file,
InputStream or InputSource. One of those also makes it possible to
parse a string rather than reading a file. And no validation against
any DTD or schema will be attempted when the factory is configured that
way.

= Steve =
 
J

James Westby

TRW said:
Thanks Steve.

Yes, I agree with that.

Is there a way to parse the document without having a definition
document
or schema?

Basically, just check pure syntax not to see if the tags are valid.

How about

http://www.perfectxml.com/JAXP.asp

Or there are many XML tutorial available on the web, you just need to
find one that shows how to using your chosen technologies.

The tutorial above uses DOM and JAXP, but there are other parsers
available. You could also use SAX if you are worried about memory. There
is the option to implement something yourself if you are really
concerned with speed and simplicity and just want to check e.g. that
every opening tag has a closing tag, this would be pretty easy. However
if you decided you wanted to do a little more later on it would be
benificial to be using something with more functionality.

James
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top