Solved

Processing xsd file with JAXB

Posted on 2011-03-04
5
563 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now