It's not a secret anymore!
The moose likes XML and Related Technologies and the fly likes New Fields in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "New Fields in XML" Watch "New Fields in XML" New topic

New Fields in XML

M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92

I am new in XML's and not sure if this is the right place to put my question. I want test XML's wherein I got to check if all the mandatory fields are present in XML or not. Also if there is any new field/node is added to the XML than expected.

Can you please help me knowing if it is possible to achieve and how it can be done.

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Hello Me,

the usual way to validate XML documents is to provide some kind of schema like a DTD or XML Schema. XML parsers and other tools allow you easily to validate if a document conforms to its schema, i.e. if the structure and content are as expected. XML Schema would be much more powerful than DTDs regarding the constraints you can define for the structure and content of valid documents.

The downside is of course that you would have to make yourself familiar with the schema language in question.

Alternatively the validation can be done completely in code but to provide a schema to validate at least the most important things is much more reasonable in my opinion. After validating against a schema you can add some more detailed validation inside your application code.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
A schema language (such as XML-Schema or RELAX-NG) can be used to make sure that an XML document is valid according to predefioned rules.

For comparing XML documents, search for "xml diff" or some such, there's a number of those out there (some are mentioned here).
M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
Thanks for the replies.. Its really helpful in the sense I have started with something now.
However I am still in problems

I have written the following sample xsd.

<xs:schema id="NewDataSet" xmlns:xs=""
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns="">
<xs:element name="input_parameters">
<xs:element name="type" type="xs:string" minOccurs="1" />
<xs:element name="param1" type="xs:string"
minOccurs="1" />
<xs:element name="param2" type="xs:string"
minOccurs="0" />

with the sampleInput.xml as

<?xml version="1.0" encoding="UTF-8"?>

The java code is to do the validation is using SAX

public static void validateSchema(String schemaURL, String xmlURL){
SAXParser parser = new SAXParser();
XMLValidationErrorHanlder errorHandler = new XMLValidationErrorHanlder();
try {
parser.setFeature("", true);
parser.setFeature("", true);

} catch (SAXNotRecognizedException e) {
} catch (SAXNotSupportedException e) {
} catch (SAXException e) {
} catch (IOException e) {


My question is, the xsd says for some elements the minOccur is 1. I haven't put any data in the xml. Still the parser is successful without giving any error. Is there any way I can findout if any required field is missing. or may be there is an extra field in the XML then that should also be detected.

I agree. Here's the link:
subject: New Fields in XML
It's not a secret anymore!