Java 如何读取Servlet请求中的xml文件
报头请求Java 如何读取Servlet请求中的xml文件,java,xml,servlets,servlet-filters,Java,Xml,Servlets,Servlet Filters,报头请求 POST http://{URL} HTTP/1.1 Accept: */* Accept-Language: en-us Content-Type: text/xml; charset=UTF-8 Accept-Encoding: gzip, deflate Content-Length: 1049 Connection: Keep-Alive Pragma: no-cache <?xml version="1.0"?> <userID>dummy</
POST http://{URL} HTTP/1.1
Accept: */*
Accept-Language: en-us
Content-Type: text/xml; charset=UTF-8
Accept-Encoding: gzip, deflate
Content-Length: 1049
Connection: Keep-Alive
Pragma: no-cache
<?xml version="1.0"?>
<userID>dummy</userID>
<password>pwd</password>
我有上面的请求,它被发送到doFilter中的ServletRequest。
我可以使用ServetAPI读取参数,如下所示
HttpServletRequest req = (HttpServletRequest) request;
Enumeration<String> params = req.getParameterNames();
如何读取xml内容以便使用Java中的servlet检索xml中的用户ID这就是我如何使用org.dom4j.io.SAXReader将xml从HTTPServletRequest读入映射的方法
我想知道这是否是一个真正的用户和密码。请注意,您所拥有的不是有效的XML。XML文档只有一个根元素。可能有重复的
public static Map<String, String> parseXml(HttpServletRequest request) throws IOException, DocumentException {
Map<String,String> map=new HashMap<String,String>();
InputStream is =request.getInputStream();
SAXReader reader=new SAXReader();
Document document=reader.read(is);
Element root =document.getRootElement();
List<Element> elementList=root.elements();
for(Element e: elementList){
map.put(e.getName(), e.getText());
}
//now your map will have below mapping
//"userID"->"dummy"
//"password"->"pwd"
return map;
}