首页 > 其他分享 >dom麻烦的写法 --> 原来已经有写好的库

dom麻烦的写法 --> 原来已经有写好的库

时间:2023-04-10 14:04:44浏览次数:37  
标签:Exception temp dom -- list System println 写法 out


自己写的:

1 保证null时,或是异常时,也要保存成默认值,如字符串是"",int是0

2 getNodeValue有throw异常,无需要往上级throw直接压掉;

所以代码写起来很长一节.有点郁闷.

---------

 

private void parseUserInfo(boolean flush) throws Exception {
        //把一级的节点键值放到对应成员变量上
        if (! flush && (this.reader.getUserInfo() != null) ){
            return ;//已获取,不必更新
        }
        
        Element node = new Api(_this.reader).get_user_info();
        
        if ( ! "member".equalsIgnoreCase(node.getTagName())){
            throw new Exception("提取会员资料出错:xml内容不包含会员资料,或格式已变更");
        }
        
        Hashtable userInfo = new Hashtable();
        
        NodeList list = node.getElementsByTagName("userID");
        
        try{

            if (list.getLength() > 0){
                int uid = 0;
                
                try{
                    uid = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
                }catch(  Exception e){
                    System.out.println("tip>parseUserInfo uid " + e);
                    uid = 0;
                }
                
                userInfo.put("id", new Integer(uid));
            }
            
            list = node.getElementsByTagName("username");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                }catch(  Exception e){
                    System.out.println("tip>parseUserInfo username " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("name", temp);
            }
            
            list = node.getElementsByTagName("realName");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                }catch(  Exception e){
                    System.out.println("tip>parseUserInfo username " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("real", temp );
            }
            
            list = node.getElementsByTagName("imoney");
            
            if (list.getLength() > 0){

                float temp = 0;
                
                try{
                    temp = Float.parseFloat(list.item(0).getFirstChild().getNodeValue());
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo imoney " + e);
                    temp = 0;
                }
                
                userInfo.put("money", new Float(temp ) );
            }
            
            list = node.getElementsByTagName("isAuthor");
            
            if (list.getLength() > 0){

                boolean temp = false;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue().trim().equals("0");
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo isAuthor " + e);
                }
                
                userInfo.put("isAuth", new Boolean(temp) );
            }
            
            list = node.getElementsByTagName("userNumber");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo userNumber " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("no.", temp);
            }
            
            list = node.getElementsByTagName("gender");
            
            if (list.getLength() > 0){

                String temp = "保密";
                
                try{
                    if (list.item(0).getFirstChild().getNodeValue().trim().equals("1")){
                        temp =  "男";
                    }else if (list.item(0).getFirstChild().getNodeValue().trim().equals("1")){
                            temp =  "女";
                    }
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo gender " + e);
                }
                
                userInfo.put("sex", temp);
            }
            
            list = node.getElementsByTagName("qq");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo qq " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("qq", temp);
            }
            
            list = node.getElementsByTagName("msn");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo msn " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("msn", temp);
            }
            
            list = node.getElementsByTagName("phone");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo phone " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("phone", temp);
            }
            
            list = node.getElementsByTagName("credit");
            
            if (list.getLength() > 0){

                int temp = 0;
                
                try{
                    temp = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo credit " + e);
                }
                
                userInfo.put("credit", new Integer(temp) );
            }
            
            list = node.getElementsByTagName("memberTitle");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo title " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("title", temp );
            }
            
            list = node.getElementsByTagName("titleImage");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo img " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("img", temp);
            }
            
            list = node.getElementsByTagName("level");
            
            if (list.getLength() > 0){

                int temp = 0;
                
                try{
                    temp = Integer.parseInt(list.item(0).getFirstChild().getNodeValue());
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo level " + e);
                }
                
                userInfo.put("level", new Integer(temp) );
            }
            
            list = node.getElementsByTagName("address");
            
            if (list.getLength() > 0){

                String temp = null;
                
                try{
                    temp = list.item(0).getFirstChild().getNodeValue();
                    
                }catch(Exception e){
                    System.out.println("tip>parseUserInfo addr " + e);
                }finally{
                    if (null == temp) temp = "";
                }
                
                userInfo.put("addr", temp);
            }
        }catch(Exception e){
            System.out.println("tip>parseUserInfo map.put " + e);
            throw new Exception("提取用户资料出错");
        }
        

        this.reader.setUserInfo(userInfo);
    }

---------

没心机为了实现这个而自己写一个统一的处理,在群中一问才知道.有人已经写好.系列成类中的成员的库

如xStream.

会自动的把xml系列成类中的成员变量;

这样就极其简单了

但是会有性能上的损失,因为通用性就损失性能

----

后来测试了一下,发现Xstream不适合bb使用.引用的不存在类太多了,修改没必要.

找到一个kxml2-min-2.3.0.jar,经过处理后就能使用.处理方式在后面文章中有

这个只是方便一点处理而已.但是却不能xml 2 class;

但是看了一下示例.发现还是要if(tagname) tagNameVar = tagValue形式;

还是没有一个统一的接口不需要判断,直接把xml转成变量

------

再找到sax2.

http://sourceforge.net/projects/sax/

回头看bb的api,发现已经有这东西......

看完了说明,发现它只是dom的替代品.只是内存使用上会更优.基于事件触发.

想了一下.发现还不适合多级的xml结构.要是想把多级的装到对象中时,会很麻烦;

测试代码:

package com.iximo.object;

import java.io.InputStream;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;


public class XmlSax extends DefaultHandler
{
    public XmlSax ()
    {
        super();
    }
    
    public void xmlParse (InputStream r)
    throws Exception
    {
        XMLReader xr = XMLReaderFactory.createXMLReader();
        XmlSax handler = new XmlSax();
        xr.setContentHandler(handler);//设置内容事件
        xr.setErrorHandler(handler);//设置出错事件,需要实现各个方法,否则使用默认的方法.有异常,警告等不同级别
        xr.parse(new InputSource(r));//解析流,可以是任何inputStream
    }

    public void startDocument (){
        System.out.println("tip>Start sax document");
    }


    public void endDocument () {
        System.out.println("tip>End sax document");
    }


    public void startElement (String uri, String name,
                  String qName, Attributes atts){
        if ("".equals (uri))
            System.out.println("Start element: " + qName);
        else
            System.out.println("Start element: {" + uri + "}" + name);
    }


    public void endElement (String uri, String name, String qName)
    {
    if ("".equals (uri))
        System.out.println("End element: " + qName);
    else
        System.out.println("End element:   {" + uri + "}" + name);
    }


    public void characters (char ch[], int start, int length)
    {
    System.out.print("Characters:    \"");
    for (int i = start; i < start + length; i++) {
        switch (ch[i]) {
        case '\\':
        System.out.print("\\\\");
        break;
        case '"':
        System.out.print("\\\"");
        break;
        case '\n':
        System.out.print("\\n");
        break;
        case '\r':
        System.out.print("\\r");
        break;
        case '\t':
        System.out.print("\\t");
        break;
        default:
        System.out.print(ch[i]);
        break;
        }
    }
    System.out.print("\"\n");
    }

}

-----

xml

<?xml version="1.0" encoding="UTF-8" ?><error><error_id>501</error_id><error_info>unthorize</error_info></error>

----输出

[0.0] tip>Start sax document
[0.0] Start element: error
[0.0] Start element: error_id
[0.0] Characters:    "501"
[0.0] End element: error_id
[0.0] Start element: error_info
[0.0] Characters:    "unthorize"
[0.0] End element: error_info
[0.0] End element: error
[0.0] tip>End sax document

 

-----

麻烦.没找到合适的....

 

标签:Exception,temp,dom,--,list,System,println,写法,out
From: https://blog.51cto.com/u_252283/6180647

相关文章

  • 透过Gartner最新报告,认识“超级边缘”
    当下,酝酿能量的超级边缘。最近,我们在谈视频化狂飙、谈AIGC颠覆、谈算力动能不足,很少谈及边缘。但“边缘”恰恰与这一切相关,且越发密不可分,它是未来技术发展的极大影响因子。 “到2025年,超过70%的组织将为其⾄少⼀个边缘计算系统,部署超⼤规模云边缘解决⽅案,并会结合其云部署......
  • 火山引擎云原生数据仓库 ByteHouse 技术白皮书 V1.0(上)
     更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群近日,《火山引擎云原生数据仓库ByteHouse技术白皮书》正式发布。白皮书简述了ByteHouse基于ClickHouse引擎的发展历程,首次详细展现ByteHouse的整体架构设计及自研核心技术,为云原生数据......
  • 内部类会暂停主类/线程创建方案
        //创建新线程处理联网动作,并更新屏幕(newThread(){publicvoidrun(){UiApplication.getUiApplication().invokeLater(newRunnable(){publ......
  • #yyds干货盘点#【愚公系列】2023年04月 .NET CORE工具案例-多语言离线翻译系统
    前言1.在线翻译在线翻译,一般是指在线翻译工具,如百度翻译、阿里翻译1688或Google翻译等。这类翻译工具的作用是利用计算机程序将一种自然语言(源语言)转换为另一种自然语言(目标语言)。其原理是依托海量的互联网数据资源和自然语言处理技术,在数百万篇文档中查找各种模式,以求解最佳......
  • 低至 200 元 / 月,火山引擎 DataLeap 帮你搭建企业级数据中台
     更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群企业数字化转型正席卷全球,这不仅是趋势所在,也是企业发展必然面对的考题。数字化不仅仅考验企业的技术“硬”实力,还考验选型、平台能力、数据部署方式等“软”技能。企业数字化转型离不开数据工......
  • #yyds干货盘点#【愚公系列】2023年04月 .NET CORE工具案例-分布式服务的健康检查系统
    前言1.健康检查系统来源背景互联网产品对用户体验提出了很高的要求,但常常由于技术侧原因,发生服务响应慢或者服务不可用等一系列影响用户体验的问题,导致业务中断,影响收入。影响服务不可用和响应慢的因素很多,可能是服务硬件损坏、光纤被挖断,可能是请求量过大导致数据库CPU负载、磁......
  • 盛元广通Sass仪器预约管理系统
    为满足科研单位以及各类院校大型仪器设备开放共享的实际业务需求,基于B/S架构,部署环境为云服务或局域网的Sass仪器预约管理系统应运而生,该系统面向不同领域用户群体,主要是辅助实验教学与科研项目智能化管理。目前大多预约仪器需要线下进行,教师在线下对仪器和学生进行管控,极大的增加......
  • 远程抄表必备神器,DTU让数据传输更快更准
    相信很多人小时候都会遇到水电局的工作人员前往家中抄水电表的情况,当时水电表都是安装在自己家里的,而通信技术不像现在这么发达,人工抄表统计也是最基本的方法之一。虽然这种方式简单,但有时候也会有意想不到的效果。比如在刑侦抓捕场景中,都会使用现在还在被广大网民用于调侃的一句话......
  • 飞桨EasyDL落地三大工业场景,工业AI赋能产业升级
    数智化时代,如何利用人工智能实现传统生产方式的转型升级,成为摆在每个工业制造企业的一道必答题。工业生产、质检、管理等环节,持续产生海量数据。以机器视觉为代表的AI技术,广泛应用在3C电子、快消品制造、汽车零部件制造等多个领域,如瑕疵质检、仓储管理、生产安全。飞桨EasyDL零门槛......
  • 快递单号查询入口,批量查询快递单号,对同一天签收的快递单号进行排序或筛选
    如何快速查询多家快递的物流,并筛选出同一天签收的单号呢?今天小编给大家分享一个新的查询技巧,下面一起来试试吧。所需工具安装一个快递批量查询高手快递单号若干操作步骤步骤1:运行【快递批量查询高手】,选择“添加单号”,弹出对话框,将单号复制粘贴进去,保存一下步骤2:保存后,所有快递单号......