Spring actual combat example of using classpathresource to load XML resources

This article describes how spring uses classpathresource to load XML resources. Share with you for your reference, as follows:

One code

package lee;
import org.springframework.core.io.ClassPathResource;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
import java.util.*;
public class ClassPathResourceTest
{
  public static void main(String[] args)
    throws Exception
  {
    // 创建一个Resource对象,从类加载路径里读取资源
    ClassPathResource cr = new ClassPathResource("book.xml");
    // 获取该资源的简单信息
    System.out.println(cr.getFilename());
    System.out.println(cr.getDescription());
    // 创建基于SAX的dom4j解析器
    SAXReader reader = new SAXReader();
    Document doc = reader.read(cr.getFile());
    // 获取根元素
    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());
      }
    }
  }
}

(II) resource documents

<?xml version="1.0" encoding="GBK"?>
<计算机书籍列表>
   <书>
      <书名>疯狂Java讲义</书名>
      <作者>李刚</作者>
   </书>
   <书>
      <书名>轻量级Java EE企业应用实战</书名>
      <作者>李刚</作者>
   </书>
</计算机书籍列表>

III. operation results

Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

I hope this article will be helpful to you in Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>