Thursday 15 April 2010

xml serialization - Can we create a java object from an xml without knowing what object it is belong to -


I have an XML content, for which I do not know which class it belongs to Need to create a dynamic Java object? Is it possible to do this? It can be a simple Java object, so we can use the Java Reflection to get value from that object. For example,

  & lt; Employee & gt; & Lt; Name & gt; Jack & lt; / Name & gt; & Lt; Designation & gt; Manager & lt; / Designation & gt; & Lt; Department & gt; Finance & lt; / Department & gt; & Lt; / Staff & gt;   

So, with this XML, I need to convert to an employee object. But, I did not have an employee class in my class path, is it possible to create an object with XML?

  file fxmlFile = new file "Employee.xml"); Documentbilder factory dbfc = documentbilderfactor.Newinstance (); DocumentBuilder debugger = DBFTC NE DocumentBuilder (); Document doctor = debugger Parse (fxma file); // Optional, but recommended // read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work.doc.getDocumentElement (). Normal ( ); System.out.println ("Root Element:" + doc.getDocumentElement (.) GetNodeName ()); Node list nList = doc.getElementsByTagName ("employee"); Println ("----------------------------"); (At Temp = 0; Temp & LT; NLIIT: ATLANMENT (); Temporary ++) {Node NND = NLIT. ITM (temporary); System.out.println ("\ n Current element:" + nNode.getNodeName ()); If (nNode.getNodeType () == node. ELEMENT_NODE) ​​{Element EElment = (Element) nNode; System.out.println ("name: -" + eElement.getElementsByTagName ("name"). Item (0) .getTextContent ()); System.out.println ("Designation: -" + eElement.getElementsByTagName ("Designation"). Item (0) .getTextContent ()); System.out.println ("Department: -" + eElement.getElementsByTagName ("Department"). Item (0) .getTextContent ()); }}    

No comments:

Post a Comment