We would like to know how to obtain Java Class name from QName.
/* w w w. j a v a 2 s . c o m*/ package example; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBIntrospector; import javax.xml.namespace.QName; public class Main { public static void main(String[] args) throws Exception { Class[] classes = new Class[3]; classes[0] = A.class; classes[1] = B.class; classes[2] = C.class; JAXBContext jc = JAXBContext.newInstance(classes); JAXBIntrospector ji = jc.createJAXBIntrospector(); Map<QName, Class> classByQName = new HashMap<QName, Class>(classes.length); for(Class clazz : classes) { QName qName = ji.getElementName(clazz.newInstance()); if(null != qName) { classByQName.put(qName, clazz); } } QName qName = new QName("http://www.example.com", "EH"); System.out.println(classByQName.get(qName)); } }
A.java
package example; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="EH", namespace="http://www.example.com") public class A { }
B.java
package example; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="BEE", namespace="urn:example") public class B { }
C.java
package example; public class C { }