@Test public void xmlToObject() { CastorSample.objectToXml(); User u = CastorSample.xmlToObject(); assertNotNull(u); assertEquals("castor", u.getUserName()); for (LoginLog log : u.getLogs()) { assertNotNull(log); assertNotNull(log.getIp()); assertNotNull(log.getLoginDate()); } }
public static User xmlToObject() { try { FileReader reader = new FileReader("out/CastorSampe.xml"); Mapping mapping = new Mapping(); String mappingFile = ResourceUtils.getResourceFullPath( CastorSample.class, "mapping.xml"); mapping.loadMapping(mappingFile); Unmarshaller unmar = new Unmarshaller(mapping); User u = (User) unmar.unmarshal(reader); System.out.println("用户名: " + u.getUserName()); for (LoginLog log : u.getLogs()) { System.out.println("访问IP: " + log.getIp()); System.out.println("访问时间: " + log.getLoginDate()); } return u; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return null; }
CastorSample { public static User xmlToObject() { try { FileReader reader = new FileReader("out/CastorSampe.xml"); Mapping mapping = new Mapping(); String mappingFile = ResourceUtils.getResourceFullPath( CastorSample.class, "mapping.xml"); mapping.loadMapping(mappingFile); Unmarshaller unmar = new Unmarshaller(mapping); User u = (User) unmar.unmarshal(reader); System.out.println("用户名: " + u.getUserName()); for (LoginLog log : u.getLogs()) { System.out.println("访问IP: " + log.getIp()); System.out.println("访问时间: " + log.getLoginDate()); } return u; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return null; } }
CastorSample { public static User xmlToObject() { try { FileReader reader = new FileReader("out/CastorSampe.xml"); Mapping mapping = new Mapping(); String mappingFile = ResourceUtils.getResourceFullPath( CastorSample.class, "mapping.xml"); mapping.loadMapping(mappingFile); Unmarshaller unmar = new Unmarshaller(mapping); User u = (User) unmar.unmarshal(reader); System.out.println("用户名: " + u.getUserName()); for (LoginLog log : u.getLogs()) { System.out.println("访问IP: " + log.getIp()); System.out.println("访问时间: " + log.getLoginDate()); } return u; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return null; }  }
CastorSample { public static User xmlToObject() { try { FileReader reader = new FileReader("out/CastorSampe.xml"); Mapping mapping = new Mapping(); String mappingFile = ResourceUtils.getResourceFullPath( CastorSample.class, "mapping.xml"); mapping.loadMapping(mappingFile); Unmarshaller unmar = new Unmarshaller(mapping); User u = (User) unmar.unmarshal(reader); System.out.println("用户名: " + u.getUserName()); for (LoginLog log : u.getLogs()) { System.out.println("访问IP: " + log.getIp()); System.out.println("访问时间: " + log.getLoginDate()); } return u; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return null; }  static User getUser(); static void objectToXml(); static User xmlToObject(); static void main(String[] args); }
CastorSample { public static User xmlToObject() { try { FileReader reader = new FileReader("out/CastorSampe.xml"); Mapping mapping = new Mapping(); String mappingFile = ResourceUtils.getResourceFullPath( CastorSample.class, "mapping.xml"); mapping.loadMapping(mappingFile); Unmarshaller unmar = new Unmarshaller(mapping); User u = (User) unmar.unmarshal(reader); System.out.println("用户名: " + u.getUserName()); for (LoginLog log : u.getLogs()) { System.out.println("访问IP: " + log.getIp()); System.out.println("访问时间: " + log.getLoginDate()); } return u; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return null; }  static User getUser(); static void objectToXml(); static User xmlToObject(); static void main(String[] args);  }