dom4j解析XML时no protocol:编码问题。

while (true) {
      String line = null;
      try {
        InputStream is = socket.getInputStream();
        byte barr[] = new byte[1024];

        int kk = is.read(barr, 0, 1024);
        if (kk == -1) {
          VisitServer.SetRecText("读取数据出错!");
        }
        else if (kk == 0) {
          VisitServer.SetRecText("读取数据为空");
        }
        else {
          line = new String(barr, 0, kk, "utf-8");
          line=line.trim();
          VisitServer.SetRecText("数据:" + line);
        }
      }
      catch (IOException e) {
        VisitServer.SetRecText("In Run has Error" + e);
        VisitServer.disconnect(this);
        VisitServer.notifyRoom();
        return;
      }

用以上功能取得line数据(<abc k="kk" />)后。

用以下代码进行解析:       //InputStream ism=new StringBufferInputStream(xmlValue);

public String GetHead(String xmlValue)
  {
    try
    {
      SAXReader myRead=new SAXReader();
      Document doc=myRead.read(xmlValue);
      Element root=doc.getRootElement();
    }catch(Exception e)
    {
      Visit.VisitServer.SetRecText(e.getMessage());
    }
但是总是提示:no protocol: <abc k="kk" />

查了好多资料,发现是字符编码的事。但是我用的字符也是utf-8。

我把字符串存到1.xml文件中。提示Invalid byte 1 of 1-byte UTF-8 sequence.

用记事本打开1.xml。另存为选择utf-8或unicode。都可以正常。

用InputStream画操作。改成下面的这样解析:

 

public String GetHead(String xmlValue)
  {
    try
    {
      File f = new File("1.xml");
      SAXReader myRead=new SAXReader();
      InputStream ism=new StringBufferInputStream(xmlValue);
      Document doc=myRead.read(ism);
      Element root=doc.getRootElement();

    }catch(Exception e)
    {
      Visit.VisitServer.SetRecText(e.getMessage());
    }

OK。了。

*****************************************

用上面的方法再次试一下。又不行了。晕。

不过又找到了一个方法:


      Document myDoc = DocumentHelper.parseText(xmlValue);
      Element root = myDoc.getRootElement();

OK。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值