andysern 2016-06-03 16:23 采纳率: 6.3%
浏览 1111

jsp xml字符串decode后不能读取

一个xml返回的数据,转换为字符串后在页面显示没有了xml节点。

有一个ReadXmlString的类,将读取的字符串转换为map,以前都运行正常,能够正常输出(测试用的一个str):

String xml = "123whl123亮亮11165074贫穷1698.0初级士官湖南常德1";

out.println(readXml.read(xml));

正常输出:
{A=123, B=whl123, C=亮亮, D=1, E=1, F=165074, G=贫穷, H=1698.0, I=初级士官, J=湖南, K=常德, L=1}

  try {  
         DocumentBuilderFactory factory = DocumentBuilderFactory  
                 .newInstance();  
         DocumentBuilder builder = factory.newDocumentBuilder();  
         Document doc = builder  
                 .parse(new InputSource(new StringReader(protocolXML)));  //parse是之前的方法名字,改为了read

         Element root = doc.getDocumentElement();  
         NodeList books = root.getChildNodes();  

        if (books != null) {  
            for (int i = 0; i < books.getLength(); i++) {  
                 Node book = books.item(i);  
                 map.put(book.getNodeName(),book.getFirstChild().getNodeValue());
                 System.out.println("节点=" + book.getNodeName() + "\ttext=" 
                         + book.getFirstChild().getNodeValue());  
             }  


         }  
     } catch (Exception e) {  
         e.printStackTrace();            
     }  
     return map;


    但是问题来了,我要连接的接口返回的xml字符串,怎么都不能转换为map,如果不encode,直接就显示的数据,decode后,也只是显示数据,而没有显示节点。我怀疑就是因为没有找到节点标识,所以不能转为map。

    如果不encode显示的就是:
    Faild 密码为空 0 0 0 

    encode后显示的就是
    %3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22+%3F%3E%3Creturnsms%3E++++%3Creturnstatus%3EFaild%3C%2Freturnstatus%3E++++%3Cmessage%3E%E9%94%99%E8%AF%AF%E7%9A%84%E6%89%8B%E6%9C%BA%E5%8F%B7%EF%BF%BD%3F%3C%2Fmessage%3E++++%3Cremainpoint%3E0%3C%2Fremainpoint%3E++++%3CtaskID%3E%3C%2FtaskID%3E++++%3CsuccessCounts%3E0%3C%2FsuccessCounts%3E%3C%2Freturnsms%3E
    (证明它确实有xml节点标识)

    decode后显示:
    Faild 密码为空 0 0 0 

    始终不能转为map。

    所以请问一下大神,这个是什么原因呢?(同时再提一个问题,decode后,怎么还是有部分字是乱码??)
  • 写回答

1条回答 默认 最新

  • 关注

    mlString的类,将读取的字符串转换为map,以前都运行正常,能够正常输出(测试用的一个str):
    String xml = "123whl123亮亮11165074贫穷1698.0初级士官湖南常德1";
    out.println(readXml.read(xml));
    正常输出:
    {A=123, B=whl123, C=亮亮, D=1, E=1, F=165074, G=贫穷, H=1698.0, I=初级士官, J=湖南, K=常德, L=1}
    try {

    DocumentBuilderFactory factory = DocumentBuilderFactory

    .newInstance();

    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder

    .parse(new InputSource(new StringReader(protocolXML))); //parse是之前的方法名字,改为了read

         Element root = doc.getDocumentElement();  
         NodeList books = root.getChildNodes();  
    
        if (books != null) {  
            for (int i = 0; i < books.getLength(); i++) {  
                 Node book = books.item(i);  
                 map.put(book.getNodeName(),book.getFirstChild().getNodeValue());
                 System.out.println("节点=" + book.getNodeName() + "\ttext=" 
                         + book.getFirstChild().getNodeValue());  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果