File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating XML using XSD and with Oracle's DOMParser and XSDBuilder

 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException in XSDBuilder:

XSDBuilder builder = new XSDBuilder();
URL url = createURL(SchemaUrl);
System.out.println("Schema url : " + url);
System.out.println("builder : " + builder);
XMLSchema schemadoc = (XMLSchema) builder.build(url);
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException is on the line:

XMLSchema schemadoc = (XMLSchema) builder.build(url);

DOMParser domParser = new DOMParser();
domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);

XSDBuilder builder = new XSDBuilder();
URL url = createURL(SchemaUrl);
XMLSchema schemadoc = (XMLSchema) builder.build(url);

domParser.setXMLSchema(schemadoc);

Validator handler = new Validator();
domParser.setErrorHandler(handler);
domParser.parse(XmlDocumentUrl);
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would suggest that the "builder" variable is null. There is no other way that line of code could throw a NullPointerException.

Of course that can't possibly be the case in the code you posted. So either you posted some other code, or you are mistaken about where the exception is being thrown. Seeing the stack trace would help.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

builder value is not null. I printed that. Here is the stack trace:

builder : oracle.xml.parser.schema.XSDBuilder@10b4b2f
java.lang.NullPointerException
at oracle.xml.parser.schema.XSDBuilder.parseComplexTypeContent(XSDBuilder.java:1372)
at oracle.xml.parser.schema.XSDBuilder.parseComplexType(XSDBuilder.java:1210)
at oracle.xml.parser.schema.XSDBuilder.parseTopLevelElem(XSDBuilder.java:625)
at oracle.xml.parser.schema.XSDBuilder.buildSchema(XSDBuilder.java:486)
at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:304)
at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:286)
at utility.DOMValidator.validateSchema(DOMValidator.java:40)
at utility.DOMValidator.main(DOMValidator.java:168)

The line DOMValidator.java:40 is the line:

XMLSchema schemadoc = (XMLSchema) builder.build(url);

Regards,
Amit Sanghai.
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I thought, you were mistaken about where the exception was being thrown. So given what you just posted, either the "url" variable contains a null reference, or when you read from the URL it refers to, you get something that is in error in some way.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The url is not null. It points to a file in the disk. It must be some other error which I am not able to get at.

When I read from the URL it refers to, I am getting something that is in error in some way, but I am not able to find it out.

Is it possible due to error in XSD?
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got the error: it was in the XSD, while defining schema for empty elements. How do you define schema for an empty element with no attribute, e.g. <book/>

For an empty element "book" with an attribute "isbn" you can write:

<xs:element name="book">
<xs:complexContent>
<xsd:attribute name="isbn" type="isbnType"/>
</xs:complexContent>
</xs:element>

What about empty element with no attribute:

<xs:element name="book">
<xs:complexContent>
..........
</xs:complexContent>
</xs:element>

Thank and regards,
Amit Sanghai.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

I want to validate the XML with the XSD and find all the XML elements which have error and append a corresponding <error></error> tag in those elements and return the whole request XML back to the client.

for e.g.:

if the request xml is:

<book>
<price>dsafdsfs</price>
<published_date>dfsfsfsf</published_date>
</book>

I want to return the following xml back to the client:

<book>
<price>dsafdsfs
<error>Invalid Price</error>
</price>
<published_date>dfsfsfsf
<error>Invalid Date</error>
</published_date>
</book>

I have the relevant XSD.

Can you help me on this?

Thanks and regards,
Amit Sanghai.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with validation using XSDBuilder is that when there is error in one element, it throws exception. It does not validate the XSML as a whole.
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is standard for XML parsers, so I am not surprised to find that validators work in a similar way.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be I will have to use XSL instead of XSD.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic