Solved

Processing xsd file with JAXB

Posted on 2011-03-04
5
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 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