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 Validating XML aginst XSD 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 "Validating XML aginst XSD" Watch "Validating XML aginst XSD" New topic
Author

Validating XML aginst XSD

Tom Stevns
Ranch Hand

Joined: Nov 20, 2001
Posts: 120
Hello Ranchers !

I have tried to validate my XML with the main method in the class ATPXercesChecker - so far rather unsuccessfully !!

So if You could give me some hints about what is wrong with my
code below I would be very thankfull.

I have got fatal error going on the first line and first column.


The the XML file that I try to validate has already been validated as OK by WSAD's XML validator.



Edit Comment:
Added the opening and closing [ code ] tags, without the spaces.

- m
[ December 01, 2004: Message edited by: Madhav Lakkapragada ]

Regards Tom Stevns, SCJP2
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Tom,

I am not ignoring this message, but frankly I don't have the resources (junit jars) to compile/run your code and see what the problem is.

Hopefully, someone else could help.
Thanks.

- m
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Damn UBB!!! < Deep sigh >

Your code seems to have the wrong URI in the setFeature API. The example code in the (JSDK 1.4) documentation uses a different URI, while attempting to perform validation.

Could this be an issue ?




- m
Tom Stevns
Ranch Hand

Joined: Nov 20, 2001
Posts: 120
Hello Madhav


Thanx for Your advice so far

Yes I have had som misunderstanding about what the parser should be
"feeded" with. At first it was feeded with the XML file where the parser
sayes:

File <The whole xml notatation> not found. But my XML is unfortunately not
showed in the WSAD-debugger console. So my message from the debugger was
File not found

I changed to feed the parser like this

String document ="";
document = "resource/ModtagKvitteringer/xerces_IN_1.xml";
parser.parse(document);

And the console output goes like this:
resource/ModtagKvitteringer/xerces_IN_1.xml is valid.

But it seems to validate for well-formnedness and IO only and not
for elements definitions in the XSD files. (I change the element values
to invalid values according to the XSD's)

I have run through Roseannes FAQ on XML, quite comprehensive I might say

So the next excersice will be to reach the external XSD :
"http://editest.forsikringenshus.dk/ATP/XMLdef/ATPSPkonti.xsd

The only thing that is given right now, is when I let WSAD's XML validator
validate the XML files


I have placed the Class below cleaned for corporate packages
(Developed on SDK 1.4)


per aspera ad astra




/*
* Created on 26-11-2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package dk.atp.sp.fritvalg.broker;

import java.io.*;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.IOException;




/**
* @author tsn
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

public class ATPXercesChecker implements ErrorHandler {

// Flag to check whether any errors have been spotted.
private boolean valid = true;

public boolean isValid() {
return valid;
}

// If this handler is used to parse more than one document,
// its initial state needs to be reset between parses.
public void reset() {
// Assume document is valid until proven otherwise
valid = true;
}

public void warning(SAXParseException exception) {

System.out.println("Warning: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());

}

public void error(SAXParseException exception) {

System.out.println("Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
// Unfortunately there's no good way to distinguish between
// validity errors and other kinds of non-fatal errors
valid = false;

}

public void fatalError(SAXParseException exception) {

System.out.println("Fatal Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());

}

String getMessage(String level, SAXParseException e) {
return ( "Parsing " + level + "\n" + "Line: " + e.getLineNumber() + "\n" + "URI: " + e.getSystemId() + "\n" + "Message: " + e.getMessage());
}



public static void main(String[] args) {

String document ="";

try {
document = "resource/ModtagKvitteringer/xerces_IN_1.xml";

XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
ATPXercesChecker handler = new ATPXercesChecker();
parser.setErrorHandler(handler);

// This is a hack to fit some long lines of code that
// follow between the margins of this printed page
String features = "http://apache.org/xml/features/";

// Turn on Xerces specific features
parser.setFeature(features + "validation/dynamic", true);
parser.setFeature(features + "validation/schema-full-checking", true);
parser.setFeature(features + "validation/warn-on-duplicate-attdef", true);
parser.setFeature(features + "validation/warn-on-undeclared-elemdef", true);
parser.setFeature(features + "continue-after-fatal-error", true);
//Turn on other features
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://editest.forsikringenshus.dk/ATP/XMLdef/ATPSPkonti.xsd");


parser.parse(document);
if (handler.isValid()) {
System.out.println(document + " is valid.");
}
else {
// If the document isn't well-formed, an exception has
// already been thrown and this has been skipped.
System.out.println(document + " is well-formed.");
}
}
catch (SAXParseException e) {
System.out.print(document + " is not well-formed at ");
System.out.println("Line " + e.getLineNumber() + ", column " + e.getColumnNumber()+ " in file " + e.getSystemId());
}
catch (SAXException e) {
System.out.println("Could not check document because " + e.getMessage());
}
catch (IOException e) {
System.out.println(
"Due to an IOException, the parser could not check " + document);
}
catch (Exception e) {
System.out.println("Due to an Exception, the parser could not check " + document);
}

}

}
Tom Stevns
Ranch Hand

Joined: Nov 20, 2001
Posts: 120
Hi again Madhav !

There is a lot of namespace in the XML, so I suppose that some namespace information must be defined in the "setFeatures" as well.

The main element goes like this:

<?xml version="1.0" encoding="iso-8859-1"?><atps:ATPSPsubmission xmlns:atps="ATPSPkonti" xmlns:FogP="DFPtypes" xmlns:spkt="ATPSPkontiTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ATPSPkonti http://editest.forsikringenshus.dk/ATP/XMLdef/ATPSPkonti.xsd">


Thanx in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating XML aginst XSD