Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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
如何在Javascript客户端中解码XML消息(由Servlet响应)?_Javascript_Java_Xml_Servlets - Fatal编程技术网

如何在Javascript客户端中解码XML消息(由Servlet响应)?

如何在Javascript客户端中解码XML消息(由Servlet响应)?,javascript,java,xml,servlets,Javascript,Java,Xml,Servlets,好的,在MyServlet.java中 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { String chat=req.getParameter("chat"); String last=req.getParameter("last"); //String text="<message>data here</

好的,在
MyServlet.java中

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String chat=req.getParameter("chat");
    String last=req.getParameter("last");

    //String text="<message>data here</message>";--> does not work because it contains <message>
    String text="....";
    resp.setContentType("text/xml");
    String xml="<?xml version=\"1.0\" ?><root>"+
    "<message id=\"0\">"+
    "<user>1</user>" +
    "<text>"+ text + "</text>"+
    "<time>10:30</time>"+
    "</message>"+
    "</root>";

    resp.getWriter().println(xml);
}
上述两个代码工作正常。然而,我遇到了一个严重的问题。也就是说,如果用户发送的消息包含类似于标记的文本,如“
”、“
”、“
”、“
”等,则该消息无效

所以我的问题是:


如何在Javascript客户端中解码XML消息(由Servlet响应)?

您需要在消息中添加转义字符,因为它包含一些预定义的XML标记

为此,请下载此库(
HtmlEscape.escapeHtml4()

以下是修改后的代码:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String chat=req.getParameter("chat");
    String last=req.getParameter("last");

    //String text="<message>data here</message>";--> does not work because it contains <message> 
    String text="....";
    text = HtmlEscape.escapeHtml4(text); // add escape characters
    resp.setContentType("text/xml");
    String xml="<?xml version=\"1.0\" ?><root>"+
    "<message id=\"0\">"+ 
    "<user>1</user>" + 
    "<text>"+ text + "</text>"+
    "<time>10:30</time>"+ 
    "</message>"+ 
    "</root>"; 

    resp.getWriter().println(xml);
} 
public void doGet(HttpServletRequest-req、HttpServletResponse-resp)
抛出IOException{
字符串chat=req.getParameter(“chat”);
字符串last=req.getParameter(“last”);
//String text=“data here”;-->不起作用,因为它包含
字符串文本=“…”;
text=HtmlEscape.escapeHtml4(text);//添加转义字符
分别为setContentType(“文本/xml”);
字符串xml=“”+
""+ 
"1" + 
“”+文本+“”+
"10:30"+ 
""+ 
""; 
resp.getWriter().println(xml);
} 
当您使用javascript在web浏览器中获取此请求时,不需要删除转义字符。Html自己做


希望它能帮助您。

您需要在消息中添加转义字符,因为它包含一些预定义的xml标记

为此,请下载此库(
HtmlEscape.escapeHtml4()

以下是修改后的代码:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String chat=req.getParameter("chat");
    String last=req.getParameter("last");

    //String text="<message>data here</message>";--> does not work because it contains <message> 
    String text="....";
    text = HtmlEscape.escapeHtml4(text); // add escape characters
    resp.setContentType("text/xml");
    String xml="<?xml version=\"1.0\" ?><root>"+
    "<message id=\"0\">"+ 
    "<user>1</user>" + 
    "<text>"+ text + "</text>"+
    "<time>10:30</time>"+ 
    "</message>"+ 
    "</root>"; 

    resp.getWriter().println(xml);
} 
public void doGet(HttpServletRequest-req、HttpServletResponse-resp)
抛出IOException{
字符串chat=req.getParameter(“chat”);
字符串last=req.getParameter(“last”);
//String text=“data here”;-->不起作用,因为它包含
字符串文本=“…”;
text=HtmlEscape.escapeHtml4(text);//添加转义字符
分别为setContentType(“文本/xml”);
字符串xml=“”+
""+ 
"1" + 
“”+文本+“”+
"10:30"+ 
""+ 
""; 
resp.getWriter().println(xml);
} 
当您使用javascript在web浏览器中获取此请求时,不需要删除转义字符。Html自己做


希望它能帮助您。

但它遇到了另一个问题,当用户键入“Text”时,消息应该是“Text”。但是,它只显示“Text”您使用过我的解决方案吗?我使用过&我看不到“您想用它做什么?”如果您在中发送
数据到这里
,那么您将收到
消息数据到这里/message
。如果您想在原始数据中检索javascript中的解码实体,但它遇到了另一个问题,当用户键入“Text”时,消息应该是“Text”。但是,它只显示“Text”您使用过我的解决方案吗?我使用过&我看不到“您想用它做什么?”如果您在中发送
数据到这里
,那么您将收到
消息数据到这里/message
。如果要在原始数据中检索,请使用javascript解码实体。