Home >Backend Development >XML/RSS Tutorial >dom4j parsing xml file code example

dom4j parsing xml file code example

Y2J
Y2JOriginal
2017-04-27 11:59:501815browse

import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
 
/**
 * xml文件解释器
 *
 * dom4j
 *
 * 需导入jar包:http://sourceforge.net/projects/dom4j/files/dom4j-2.0.0-ALPHA-2/
 *
 * DOM4J使用接口和抽象基本类方法。DOM4J大量使用了API中的Collections类,但是在许多情况下,
 * 它还提供一些替代方法以允许更好的性能或更直接的编码方法。直接好处是,虽然DOM4J付出了更复杂的API的代价,但是它提供了比JDOM大得多的灵活性。
 *
 * 在添加灵活性、XPath集成和对大文档处理的目标时,DOM4J的目标与JDOM是一样的:针对Java开发者的易用性和直观操作。
 * 它还致力于成为比JDOM更完整的解决方案,实现在本质上处理所有Java/XML问题的目标。在完成该目标时,它比JDOM更少强调防止不正确的应用程序行为。
 */
public class MyXMLReader {
 
    public static void main(String arge[]) {
        long lasting = System.currentTimeMillis();
        try {
            File f = new File("data.xml");
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element root = doc.getRootElement();
            Element foo;
            for (Iterator i = root.elementIterator("VALUE"); i.hasNext(); i++) {
                foo = (Element) i.next();
                System.out.print("车牌号码:" + foo.elementText("NO"));
                System.out.println("车主地址:" + foo.elementText("ADDR"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The above is the detailed content of dom4j parsing xml file code example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn