File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

New Fields in XML

M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
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
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41843
    
  63
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).


Ping & DNS - my free Android networking tools app
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="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.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New Fields in XML