使用JAXB實現。
一些的參考文獻有:
1>code geekers
2>techferry
3>vogella
4>java papers
String USER_DETAILS_XML = "./user-details.xml";
String USER_ERROR_XML = "./user-error.xml";
public String login(String username, String password)
throws JAXBException, PropertyException, FileNotFoundException {
User user = new User();
InvalidUser invalidUser = new InvalidUser();
if ((username !=null && password !=null)) {
user.setUserName(username);
user.setFirstName("Jose");
user.setLastName("Tom");
JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(user, System.out);
marshaller.marshal(user,new File(USER_DETAILS_XML));
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
User userResult = (User) jaxbUnmarshaller.unmarshal(new FileReader(
USER_DETAILS_XML));
return userResult.getFirstName();
}
else{
invalidUser.setCode(400);
invalidUser.setMessage("something wrong here");
JAXBContext jaxbContext = JAXBContext.newInstance(InvalidUser.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(invalidUser, System.out);
marshaller.marshal(invalidUser, new File(USER_ERROR_XML));
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InvalidUser invalidUserResult = (InvalidUser) jaxbUnmarshaller.unmarshal(new FileReader(USER_ERROR_XML));
return invalidUserResult.getMessage();
}
}
User.java:
@XmlRootElement(name = "user")
@XmlType(propOrder = { "userName", "firstName", "lastName" })
public class User{
private String userName;
private String firstName;
private String lastName;
....get/set
}
InvalidUser。java:
@XmlRootElement(name = "status")
@XmlType(propOrder = { "code", "message" })
public class InvalidUser {
private int code;
private String message;
...set/get
}
爲什麼不用XML解析器代替解析文本索引 –
爲什麼不使用DOM/SAX來處理XML? – fnt