创建、传递和解析XML文件

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
...
//create document
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "saleItems" );
Element collectTime = root.addElement("collectTime").addText(saleItems.get(0).getCollectTime());
Element collectMethod = root.addElement("collectMethod").addText(saleItems.get(0).getCollectMethod());
Element si=root.addElement("saleItem");
Element commBarcode=si.addElement("commBarcode").addText(saleItem.getCommBarcode());
return document;
...
//send xml to jboss server
String saleItemsXml = documents.asXML();
...
import org.springframework.jms.core.JmsTemplate;
JmsTemplate jt = (JmsTemplate) SpringBeanFactory.getJmsBean("jmsTemplate");
jt.convertAndSend(saleItemsXml);

//receive xml from jboss server
JmsTemplate jt = (JmsTemplate) SpringBeanFactory.getJmsBean("jmsTemplate");
jt.setReceiveTimeout(1200000);
String s = (String) jt.receiveAndConvert();
Document document = DocumentHelper.parseText(s);

//parse xml to object
EnterpriseRunning enterpriseRunning = new EnterpriseRunning();
enterpriseRunning.setCollectionDate(saleItemsDocument.selectSingleNode("//saleItems/collectTime").getText());	
enterpriseRunning.setAllocatedRate(saleItemsDocument.selectSingleNode("//saleItems/percentage").getText());  
List list = saleItemsDocument.selectNodes("//saleItems/saleItem");
Iterator it = list.iterator();
while (it.hasNext()) {
       Node node = (Node) it.next();
       enterpriseRunning.setAreaCode(node.selectSingleNode("areaCode").getText());
       break;
}

/** jboss connection config xml in spring **/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="connectionFactory"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>ConnectionFactory</value>
		</property>
		<property name="jndiTemplate">
			<ref bean="jndiTemplate" />
		</property>
	</bean>
	<bean id="destination"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>queue/testQueue</value>
		</property>
		<property name="jndiTemplate">
			<ref bean="jndiTemplate" />
		</property>
	</bean>
	<bean id="jmsTemplate"
		class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory">
			<ref bean="connectionFactory" />
		</property>
		<property name="defaultDestination">
			<ref bean="destination" />
		</property>
		<property name="messageConverter">
			<ref bean="xmlMessageConverter" />
		</property>
	</bean>
	<bean id="jndiTemplate"
		class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.factory.initial">
					org.jnp.interfaces.NamingContextFactory
				</prop>
				<prop key="java.naming.provider.url">
					jnp://10.0.50.233:1099
				</prop>
				<prop key="java.naming.factory.url.pkgs">
					org.jboss.naming:org.jnp.interfaces
				</prop>
			</props>
		</property>
	</bean>
	<bean id="xmlMessageConverter" class="com.benteng.wcqx.commons.business.collection.saleitem.jms.XmlMessageConverter">
</bean>
</beans>

相关推荐