吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_ByteArrayResource
作者:互联网
<?xml version="1.0" encoding="GBK"?> <project name="spring" basedir="." default=""> <property name="src" value="src"/> <property name="dest" value="classes"/> <path id="classpath"> <fileset dir="../../lib"> <include name="**/*.jar"/> </fileset> <pathelement path="${dest}"/> </path> <target name="compile" description="Compile all source code"> <delete dir="${dest}"/> <mkdir dir="${dest}"/> <copy todir="${dest}"> <fileset dir="${src}"> <exclude name="**/*.java"/> </fileset> </copy> <javac destdir="${dest}" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <src path="${src}"/> <classpath refid="classpath"/> <compilerarg value="-Xlint:deprecation"/> </javac> </target> <target name="run" description="Run the main class" depends="compile"> <java classname="lee.ByteArrayResourceTest" fork="yes" failonerror="true"> <classpath refid="classpath"/> </java> </target> </project>
package lee; import org.springframework.core.io.ByteArrayResource; import org.dom4j.*; import org.dom4j.io.*; import java.util.*; import java.util.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class ByteArrayResourceTest { public static void main(String[] args) throws Exception { String file = "<?xml version='1.0' encoding='GBK'?>" + "<计算机书籍列表><书><书名>疯狂Java讲义" + "</书名><作者>李刚</作者></书><书><书名>" + "轻量级Java EE企业应用实战</书名><作者>李刚" + "</作者></书></计算机书籍列表>"; byte[] fileBytes = file.getBytes(); // 以字节数组作为资源来创建Resource对象 ByteArrayResource bar = new ByteArrayResource(fileBytes); // 获取该资源的简单信息 System.out.println(bar.getDescription()); // 创建基于SAX的dom4j解析器 SAXReader reader = new SAXReader(); Document doc = reader.read(bar.getInputStream()); // 获取根元素 Element el = doc.getRootElement(); List l = el.elements(); // 遍历根元素的全部子元素 for (Iterator it = l.iterator();it.hasNext() ; ) { // 每个节点都是<书>节点 Element book = (Element)it.next(); List ll = book.elements(); // 遍历<书>节点的全部子节点 for (Iterator it2 = ll.iterator();it2.hasNext() ; ) { Element eee = (Element)it2.next(); System.out.println(eee.getText()); } } } }
标签:bar,JAVA,it2,Element,企业应用,import,ByteArrayResource,节点,轻量级 来源: https://www.cnblogs.com/tszr/p/12372191.html