• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New Fields in XML

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Thanks,
me
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Marco
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns="">
<xs:element name="input_parameters">
<xs:complexType>
<xs:sequence>
<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" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

with the sampleInput.xml as

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


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();
parser.setErrorHandler(errorHandler);
try {
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schemaURL);
parser.parse(xmlURL);

} catch (SAXNotRecognizedException e) {
e.printStackTrace();
} catch (SAXNotSupportedException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}



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.



 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic