zoukankan      html  css  js  c++  java
  • XML字符串解析

    不多说,直接上代码:

    import java.io.StringReader;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    import org.xml.sax.InputSource;
    
    //转化XML字符串
    
    public Element parseXML(String strXML){
      Element source = null;
      strXML =this.transFromXmlStr(strXML);
      if("".equals(strXML)||"null".equals(strXML)){
      return source;
      }
      SAXReader reader = new SAXReader();
      StringReader sr = new StringReader(strXML);
      InputSource is = new InputSource(sr);
      Document document;
      try {
        document = reader.read(is);
        source = document.getRootElement();
      } catch (DocumentException e) {
        e.printStackTrace();
      }
      return source;
    }
    
    //特殊字符处理
    
    public static String transToXmlStr(String text) {
      if (text == null)
      return "";
      String tmp = text.replace(">", "&rt;");
      tmp = tmp.replace(""", """);
      tmp = tmp.replace("<", "&lt;");
      tmp = tmp.replace("
    ", "&#13;");
      tmp = tmp.replace("
    ", "&#10;");
      tmp = tmp.replace("&", "&amp;");
      return tmp;
    }
    
    //获取XML信息
    
    public void getXML(Element root){  
    
      if(root!=null){
      Element rwxx = root.element("rwxx");
      List<Element> rows = rwxx.elements("row");
      for(Element row : rows){
        Element id = row.element("id");
    
        Element name = row.element("name");
    
        if(id!=null){
    
          System.out.println("id:"+id.getText());
    
        }
    
        if(name!=null){
    
          System.out.println("name:"+name.getText());
    
        }
    
      }
    
    }
    版权声明:如需转载,请注明!PS:如是转载随便,请忽略
  • 相关阅读:
    Python NameError: name 'include' is not defined [closed]
    Python3 venv 创建虚拟环境
    python编程:从入门到实践读书笔记(一)
    kafka(2.2.1)(kerberos+LDAP+Sentry)访问使用
    实现Base64解码和命令分发器
    装饰器器应用及用途
    __slots__和运算符重载中的反向方法
    python插件化开发
    python分发包管理
    SocketServer模块
  • 原文地址:https://www.cnblogs.com/zwdx/p/7193536.html
Copyright © 2011-2022 走看看