?
Solved

Processing xsd file with JAXB

Posted on 2011-03-04
5
Medium Priority
?
572 Views
Last Modified: 2012-05-11
Hello!

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.setSchema(schema);         
		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!

thanks,
Ron
0
Comment
Question by:ronyosi
  • 3
5 Comments
 
LVL 1

Expert Comment

by:GenroseSusai
ID: 35037879
For e.g...  


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

Author Comment

by:ronyosi
ID: 35039126
Hi,

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); "
and
SomeField/SomeType does not work because java does not know what that is...

Ron
0
 
LVL 92

Expert Comment

by:objects
ID: 35040897
0
 

Accepted Solution

by:
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 :)
0
 

Author Closing Comment

by:ronyosi
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 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