Processing xsd file with JAXB

Posted on 2011-03-04
Medium Priority
Last Modified: 2012-05-11

I would like to process an xsd file using JAXB and get to the first element to find its name (in the schema itself. How would I go about doing this in JAXB? It should be a few lines of code...

So far I have:

package blog.jaxb.validation;   
import java.io.File; 
import javax.xml.XMLConstants; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory;   
public class UnmarshalDemo 
	public static void main(String[] args) throws Exception {         
		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);          
		Schema schema = sf.newSchema(new File("customer.xsd"));            
		JAXBContext jc = JAXBContext.newInstance(Customer.class);           
		Unmarshaller unmarshaller = jc.createUnmarshaller();         
		unmarshaller.setEventHandler(new MyValidationEventHandler());         
		Customer customer = (Customer) unmarshaller.unmarshal(new File("input.xml"));     

Open in new window

Thing is, all the examples that I saw are for using the xsd to validate xml. Here what I am trying to do is access the information in the schema itself!

Question by:ronyosi
  • 3

Expert Comment

ID: 35037879
For e.g...  

JAXBElement<?> poElement =
      new FileInputStream( "input.xml" ) );
   SomeType st = (
   SomeField field = st.getSomeField();  

Author Comment

ID: 35039126

Thanks for the reply :)
How do I avoid this SomeType/SomeField.
Note that the schemas that I will be loading are somewhat random and only have in common that specific tag that I am trying to inspect. Therefore the
"JAXBContext jc = JAXBContext.newInstance(Customer.class); "
SomeField/SomeType does not work because java does not know what that is...

LVL 92

Expert Comment

ID: 35040897

Accepted Solution

ronyosi earned 0 total points
ID: 35201941
Ok so I used DOM4J and it is useful in that you can use XPATH expressions to find the node that is required.

thanks :)

Author Closing Comment

ID: 35230119
thanks everyone for your help. I found an answer on google that showed me why to pick DOM4J that it is actually less expensive in terms of memory/computation.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
A discussion about automated testing of Web Applications utilizing Selenium, along with illustrated configuration steps for the Jenkins open source tool.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question