javaBean使用注解方式转成xml中,给元素添加CDATA的方法

1 篇文章 0 订阅

该注解使用的是jdk自带的rt.jar包下的

1、javaBean注解如下:

@XmlAccessorType(XmlAccessType.FIELD)  
@XmlRootElement(name="process")
public class Process extends BaseAttribute{
	@XmlAttribute(name="isExecutable")  
	String isExecutable;

	@XmlElement(name = "sequenceFlow") 
	List<SequenceFlow> sequenceFlows;//线条集合
	
	public Process() {  
        super();  
    }  
  
    public Process(String id,String name,String documentation,String isExecutable,
    		List<SequenceFlow> sequenceFlows) {  
    		super(id, name, documentation);  
        this.sequenceFlows = sequenceFlows;
        
    }  
    
	public String getIsExecutable() {
		return isExecutable;
	}

	public void setIsExecutable(String isExecutable) {
		this.isExecutable = isExecutable;
	}
	
	public List<SequenceFlow> getSequenceFlows() {
		return sequenceFlows;
	}
	public void setSequenceFlows(List<SequenceFlow> sequenceFlows) {
		this.sequenceFlows = sequenceFlows;
	}
}
@XmlAccessorType(XmlAccessType.FIELD)  
public class SequenceFlow extends BaseAttribute{
	
	@XmlAttribute(name="sourceRef")  
	String sourceRef;//源端
	
	@XmlAttribute(name="targetRef")  
	String targetRef;//目标端
	
	@XmlElement(name = "conditionExpression")  
	ConditionExpression conditionExpression;

	public SequenceFlow() {
		super();
	}

	public SequenceFlow(String id, String name,String documentation,String sourceRef,
			String targetRef,ConditionExpression conditionExpression) {
		super(id,name,documentation);
		this.sourceRef = sourceRef;
		this.targetRef = targetRef;
		this.conditionExpression = conditionExpression;
	}
	
	public String getSourceRef() {
		return sourceRef;
	}
	
	public void setSourceRef(String sourceRef) {
		this.sourceRef = sourceRef;
	}
	
	
	public String getTargetRef() {
		return targetRef;
	}
	public void setTargetRef(String targetRef) {
		this.targetRef = targetRef;
	}

	
	public ConditionExpression getConditionExpression() {
		return conditionExpression;
	}

	public void setConditionExpression(ConditionExpression conditionExpression) {
		this.conditionExpression = conditionExpression;
	}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class ConditionExpression {
	
	@XmlAttribute(name="xsi:type")  
	String type;

	@XmlElement(name="conditionValue")  
	String conditionValue;
	
	public ConditionExpression(){
    }
    public ConditionExpression(String type,String conditionValue) {
        this.type = type;
        this.conditionValue = conditionValue;
    }
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	public String getConditionValue() {
		return conditionValue;
	}
	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
}

@XmlAccessorType(XmlAccessType.FIELD)  
public class BaseAttribute {
	@XmlAttribute(name="id")  
	public String id;//节点id
	@XmlAttribute(name="name")  
	public String name;//节点名称
	@XmlElement(name="documentation")  
	public String documentation;//描述信息
	
	public BaseAttribute(){
    }
    public BaseAttribute(String id,String name,String documentation) {
    	this.id = id;
    	this.name = name;
        this.documentation = documentation;
    }

	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDocumentation() {
		return documentation;
	}

	public void setDocumentation(String documentation) {
		this.documentation = documentation;
	}
}

2、生成xml的方法,这里注意将所有需要添加的元素都加到setCDataElements数组内,如下代码所示:

@SuppressWarnings("restriction")
public class BeanToXml2 {
    
    /**
     * java对象转换为xml文件
     * @param xmlPath  xml文件路径
     * @param load    java对象.Class
     * @return    xml文件的String
     * @throws JAXBException    
     */
    @SuppressWarnings("restriction")
	public static String beanToXml(Object obj,Class<?> load) throws Exception{
        JAXBContext context = JAXBContext.newInstance(load);
        
        OutputFormat of = new OutputFormat(); 
        of.setCDataElements( 
        		new String[] { "^conditionValue"});
        
        of.setPreserveSpace(true); 
        of.setIndenting(true);
        ByteArrayOutputStream op = new ByteArrayOutputStream(); 
        XMLSerializer serializer = new XMLSerializer(op, of); 
        SAXResult result = new SAXResult(serializer.asContentHandler()); 
       
        Marshaller mar = context.createMarshaller(); 
        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
        mar.marshal(obj, result); 
        return op.toString("UTF-8");
    }
    
    /**
    * @Title: getProcess
    * @Description: TODO(流程定义部分)
    * @return
    * @return Process (这里用一句话描述返回结果说明)
    */ 
    public Process getProcess(){
    	Process process = new Process();//2
    	process.setId("processId");
    	process.setName("名称");
    	process.setIsExecutable("true");
    	process.setSequenceFlows(getSequenceFlows());
    	return process;
    }
    
    public List<SequenceFlow> getSequenceFlows(){
    	List<SequenceFlow> sequenceFlows = new ArrayList<SequenceFlow>();//线条集合  3.5
    	
    	ConditionExpression conditionExpression1 = new ConditionExpression("tFormalExpression","${pass}");
    	ConditionExpression conditionExpression2 = new ConditionExpression("tFormalExpression","${!pass}");
    	
    	SequenceFlow sequenceFlow1 = new SequenceFlow("flow10","同意",null,
    			"sourceId1","target1",conditionExpression1);
    	
    	SequenceFlow sequenceFlow2 = new SequenceFlow("flow11","退回",null,
    			"sourceId2","target2",conditionExpression2);

    	sequenceFlows.add(sequenceFlow1);
    	sequenceFlows.add(sequenceFlow2);
    	return sequenceFlows;
    }

    public static void main(String[] args) throws Exception {

    	BeanToXml2 beanToXml = new BeanToXml2();
    	
    	String str =  BeanToXml2.beanToXml(beanToXml.getProcess(), Process.class);
    	System.out.println(str);
        //写入到xml文件中
        String xmlPath = "D:/testConfig.bpmn";
        BufferedWriter bfw = new BufferedWriter(new FileWriter(new File(xmlPath)));
        bfw.write(str);
        bfw.close();
    }
}

3、最终生成的xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<process isExecutable="true" id="processId" name="名称">
	<sequenceFlow sourceRef="sourceId1" targetRef="target1" id="flow10" name="同意">
		<conditionExpression xsi:type="tFormalExpression">
			<conditionValue><![CDATA[${pass}]]></conditionValue>
		</conditionExpression>
	</sequenceFlow>
	<sequenceFlow sourceRef="sourceId2" targetRef="target2" id="flow11" name="退回">
		<conditionExpression xsi:type="tFormalExpression">
			<conditionValue><![CDATA[${!pass}]]></conditionValue>
		</conditionExpression>
	</sequenceFlow>
</process>

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当将一个Map换为指定类型的JavaBean对象时,可以使用反射来实现。下面是一个示例的静态方法,它将一个Map换为指定类型的JavaBean对象: ```java import java.lang.reflect.Field; import java.util.Map; public class MapToBeanConverter { public static <T> T convertMapToBean(Map<String, Object> map, Class<T> clazz) throws Exception { T bean = clazz.newInstance(); for (Map.Entry<String, Object> entry : map.entrySet()) { String fieldName = entry.getKey(); Object value = entry.getValue(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(bean, value); } return bean; } } ``` 使用示例: ```java import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) throws Exception { Map<String, Object> map = new HashMap<>(); map.put("name", "John"); map.put("age", 25); Person person = MapToBeanConverter.convertMapToBean(map, Person.class); System.out.println(person.getName()); // 输出: John System.out.println(person.getAge()); // 输出: 25 } } ``` 以上代码,我们定义了一个`MapToBeanConverter`类,其的`convertMapToBean`方法接受一个`Map<String, Object>`类型的参数和一个目标类型的`Class<T>`参数。该方法使用反射遍历Map的键值对,通过反射设置目标类型对象的属性值,最后返回换后的JavaBean对象。 请注意,上述代码只是一个示例,可能需要根据具体的需求进行修改和适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值