Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取Servlet请求中的xml文件_Java_Xml_Servlets_Servlet Filters - Fatal编程技术网

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