⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oxmlmapping.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			//dange de 
			/**
			 * 保存xml文件的节点数据---字段
			 * 1,
			 */
			 INode node=nodes.get(0);
			 
			field.setAccessible(true);
			String nodeType=node.getAttributes().get(IOXmlMapping.CLASSNAME);
			Class clazz=Class.forName(nodeType);
			Object object=clazz.newInstance();
			/**
			 * 属性
			 */
			 
			Field[] childFields=clazz.getDeclaredFields();
			for(int i=0;i<childFields.length;i++){
				childFields[i].setAccessible(true);
	            XMLAttribute xMLAttribute=childFields[i].getAnnotation(XMLAttribute.class);
				
				if(xMLAttribute!=null){
					if("".equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, node.getAttributes().get(childFields[i].getName()));
						}else{
							childFields[i].set(object, node.getAttributes().get(xMLAttribute.name()));
						}
						
						
					}else{
						/**
						 * 如果是工具类提供的方法之一,就执行工具类
						 */
						
						if(IConvertCastUtil.castBoolean.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castBoolean(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castBoolean(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
											
						}else if(IConvertCastUtil.castDouble.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castDouble(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castDouble(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castFloat.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castFloat(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castFloat(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castInt.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castInt(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castInt(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castLong.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castLong(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castLong(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castShort.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castShort(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castShort(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castWrappedBoolean.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castWrappedBoolean(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castWrappedBoolean(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
											
						}else if(IConvertCastUtil.castWrappedDouble.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castWrappedDouble(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castWrappedDouble(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castWrappedFloat.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castWrappedFloat(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castWrappedFloat(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castWrappedInt.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castWrappedInt(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castWrappedInt(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castWrappedLong.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castWrappedLong(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castWrappedLong(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else if(IConvertCastUtil.castWrappedShort.equals(xMLAttribute.castMethod())){
							if("".equals(xMLAttribute.name())){
								childFields[i].set(object, this.getConvertCastUtil().castWrappedShort(node.getAttributes().get(childFields[i].getName())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, this.getConvertCastUtil().castWrappedShort(node.getAttributes().get(xMLAttribute.name())));
								//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
						}else{
						
						//下面是业务类自己定义的转换方法
						Method castMethod=clazz.getDeclaredMethod(xMLAttribute.castMethod(), String.class);
						castMethod.setAccessible(true);
						if("".equals(xMLAttribute.name())){
						
							childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
						}
						
					}
				
				}
				/**
				 * 子节点
				 * 
				 */
				XMLNode xMLNode=childFields[i].getAnnotation(XMLNode.class);
				if(xMLNode!=null){
					//是子节点   并且不是null
					//if(fields[i].get(rootObject)!=null){}
					/**
					 *是哪一个子节点?
					 * 1,
					 * 
					 * 
					 */
					//node.getChildNodes().
					List<INode> childNodes=null;
					if("".equals(xMLNode.name())){
						childNodes=node.getChildNodes(this.getLocalClassName(childFields[i].getType()));
						
					}else{
						childNodes=node.getChildNodes(xMLNode.name());
					}
					this.upCastRecursion(object,childNodes, childFields[i]);
									
				}
				
			}
			
			
			
			//组装
			field.set(parent, object);
			
			
			
		}
				
		
			
			
		
		
		
	}
	
	
	
	/**
	 * 递归,转换子节点为对象
	 * @param parent  field的外部对象
	 * @param node  节点数据
	 * @param field 字段数据
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	
	private void upCastRecursion(Object parent,INode node,Field field) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
		/**
		 * 保存xml文件的节点数据---字段
		 * 1,
		 * 
		 
		field.setAccessible(true);
		String nodeType=node.getAttributes().get(IOXmlMapping.CLASSNAME);
		Class clazz=Class.forName(nodeType);
		Object object=clazz.newInstance();
		/**
		 * 属性
		 * 
		 
		Field[] childFields=clazz.getDeclaredFields();
		for(int i=0;i<childFields.length;i++){
			childFields[i].setAccessible(true);
            XMLAttribute xMLAttribute=childFields[i].getAnnotation(XMLAttribute.class);
			
			if(xMLAttribute!=null){
				if("".equals(xMLAttribute.castMethod())){
					if("".equals(xMLAttribute.name())){
						childFields[i].set(object, node.getAttributes().get(childFields[i].getName()));
					}else{
						childFields[i].set(object, node.getAttributes().get(xMLAttribute.name()));
					}
					
					
				}else{
					/**
					 * 如果是工具类提供的方法之一,就执行工具类
					 * 
					
					if(IConvertCastUtil.castBoolean.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, this.getConvertCastUtil().castBoolean(node.getAttributes().get(childFields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, this.getConvertCastUtil().castBoolean(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
										
					}else if(IConvertCastUtil.castDouble.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, this.getConvertCastUtil().castDouble(node.getAttributes().get(childFields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, this.getConvertCastUtil().castDouble(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
					}else if(IConvertCastUtil.castFloat.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, this.getConvertCastUtil().castFloat(node.getAttributes().get(childFields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, this.getConvertCastUtil().castFloat(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
					}else if(IConvertCastUtil.castInt.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, this.getConvertCastUtil().castInt(node.getAttributes().get(childFields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, this.getConvertCastUtil().castInt(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
					}else if(IConvertCastUtil.castLong.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, this.getConvertCastUtil().castLong(node.getAttributes().get(childFields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, this.getConvertCastUtil().castLong(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
					}else if(IConvertCastUtil.castShort.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							childFields[i].set(object, this.getConvertCastUtil().castShort(node.getAttributes().get(childFields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							childFields[i].set(object, this.getConvertCastUtil().castShort(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
					}else if(IConvertCastUtil.castWrappedBoolean.equals(xMLAttribute.castMethod())){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -