|
|
|||
Get class type from xml content
I have created sort of .xml files which contains XXbean.class datas .But the task which I need to do is perform comparison between data from xml file and data that is in database.
Only parameter that I have xml url.But I retrieve information from xml file and return as Object class but instead I require to return type of related bean class. One part of UlkeBean.xml file. Each xml file created with the name of relevant class. --------------------------------- <?xml version='1.0'?> <root id="1"> <UlkeBean id="2"> <aktifMi>E</aktifMi> <IlBean id="27"> <aktifMi>E</aktifMi> <ilAdi>Diyarbakır</ilAdi> <ilId/> <ilKodu>0</ilKodu> <kulId>0</kulId> <timeStamp id="28"> <firstDayOfWeek>1</firstDayOfWeek> <gregorianChange>Fri Oct 15 02:00:00 EET 1582</gregorianChange> <lenient>true</lenient> <minimalDaysInFirstWeek>1</minimalDaysInFirstWeek> <time>Tue Mar 30 13:27:42 EEST 2010</time> <timeInMillis>1269944862531</timeInMillis> <timeZone idref="5"/> </timeStamp> <ulke idref="2"/> <version>0</version> </IlBean> <kulId>0</kulId> <timeStamp id="29"> <firstDayOfWeek>1</firstDayOfWeek> <gregorianChange>Fri Oct 15 02:00:00 EET 1582</gregorianChange> <lenient>true</lenient> <minimalDaysInFirstWeek>1</minimalDaysInFirstWeek> <time>Tue Mar 30 13:27:39 EEST 2010</time> <timeInMillis>1269944859531</timeInMillis> <timeZone idref="5"/> </timeStamp> <ulkeAdi>Turkey</ulkeAdi> <ulkeId/> <ulkeKodu>Ab</ulkeKodu> <version>0</version> </UlkeBean> </root> ----------------------------------------------------------- The code snippet to read .xml file that I am using is below public <T extends BaseBean> void readFromXML(String beanClassName) throws IntrospectionException, SAXException, Exception { nodeBeanArrayList = new ArrayList<Node>(); System.out.println("control point 1::" + beanClassName); File file = new File("src/com/yuvamgroup/sonuc/" + beanClassName + ".xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element " + doc.getDocumentElement().getNodeName()); NodeList nodeLst = doc.getElementsByTagName(beanClassName); System.out.println("Information of all beans"); StringTokenizer st = new StringTokenizer(beanClassName, "/"); String className = null; while (st.hasMoreTokens()) { className = st.nextToken(); } className = className.substring(0, className.length() - 3); try { // Class c = Class.forName(className); // Dynamically load the class // Object o = c.newInstance(); // Dynamically instantiate it // // // Class cls = o.getClass(); // Class classDefinition = Class.forName(cls.getName()); // Object newClassObject = classDefinition.newInstance();// XStream xs = new XStream(); FileInputStream fis = new FileInputStream("src/com/yuvamgroup/sonuc/" + lst.get(0).getClass ().getSimpleName() + ".xml"); xs.getReflectionProvider(); Object r= xs.fromXML(fis); } catch (Exception e) { /* Handle exceptions */ } } ----------------------------------------------------------------------------- Object r= xs.fromXML(fis); it copy all content to object type but I need get it with the type of (for this example) UlkeBean.class .And each time given url is changing indicates different bean classes.Is there any way to return (for this case UlkeBean) class object that filled information from xml file.I have tried reflection options but I was not able to get it . 1 Reply |
|||
|