-1

my requirement is: A form submit should trigger the LoginServlet, from which the username and password are passed to UserService.login. If the username and password are validated (if statement is fine) an user xml string should be returned. Do I need to use Jaxb or any other parser? using String builder is good approach?

I have used String builder as:

StringBuilder validUser = new StringBuilder();  
validUser.append("<username>");
validUser.append(username);
validUser.append("</username>");
validUser.append("<firstname>");
validUser.append("Jose");
validUser.append("</firstname>");
validUser.append("<lastname>");
validUser.append("Tom");
validUser.append("</lastname>");
validUser.append("</user>");
String result = validUser.toString();

From the above string i.e result, I need to get/display firstName i.e.Jose to the UI. I tried with String index, not able to get exact result since user name is always changes or user name length is always varies.

4

3 回答 3

2

首先,注意注释,XML 解析器确实是正确的方法。但是,如果您只想快速修复您的代码,那么就在这里,此代码段将提取必要的信息:

Matcher matcher =  Pattern.compile("<firstname>(.*?)</firstname>").matcher(result);
matcher.find();
System.out.println(matcher.group(1));
于 2013-07-27T20:52:47.823 回答
2

使用 JAXB 实现。

一些参考是:

1>代码极客

2>技术渡轮

3>沃盖拉

4> java论文

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();
            }
}

用户.java:

@XmlRootElement(name = "user")
@XmlType(propOrder = { "userName", "firstName", "lastName" })
public class User{

    private String userName;
    private String firstName;
    private String lastName;
    ....get/set

}

无效用户.java:

@XmlRootElement(name = "status")
@XmlType(propOrder = { "code", "message" })
public class InvalidUser {
    private int code;
    private String message;
    ...set/get
}
于 2013-07-27T23:45:53.613 回答
1

首先,如果这是通过 生成有效 XML 的尝试,那么StringBuilder您的方法是值得商榷的。

另请参阅对您的问题的评论(由Jigar Joshifnt 撰写)。

有许多 API 可以以编程方式构建 XMl。在您的情况下,由于它是一个简短的文档,我建议您使用DOM

其次,如果您沉迷于松散的方法并希望通过这种方法构建您的伪 xml,您至少需要在StringBuilder初始化之后添加以下行(或作为构造函数的参数):

validUser.append("<user>");

或者

StringBuilder validUser = new StringBuilder("<user>");

最后,要“解决”您的问题,您可以使用:

String xml = validUser.toString();
System.out.println(
    xml.substring(
        xml.indexOf("<firstname>") + 11, xml.indexOf("</firstname>")
    )
);
于 2013-07-27T20:55:17.273 回答