dom4j(xpath)解析xml一例


import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Main {
    public static void main(String[] args) {
        SAXReader saxReader = null;
        Document doc = null;
        try {
            saxReader = new SAXReader();
            doc = saxReader.read(new File("D:/ss.xml"));
            List<Element> list = doc.selectNodes("//result//value");
            System.out.println(list + "list");
            for (Element EntryItem : list) {
                System.out.println(EntryItem.getName());
                String table = EntryItem.elementTextTrim("table");
                String sql = EntryItem.elementTextTrim("sql");
                System.out.println("table=" + EntryItem.elementTextTrim("table"));
                System.out.println("sql=" + EntryItem.elementTextTrim("sql"));
               

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 运行结果为:

[org.dom4j.tree.DefaultElement@18f6235 [Element: <value attributes: [org.dom4j.tree.DefaultAttribute@1dfc547 [Attribute: name name value "value"]]/>]]list
value
table=tbl_notice
sql=select title,content from tbl_notice order by id desc
 

 

ss.xml文件的内容为:

<?xml version="1.0" encoding="UTF-8"?>

<result>
<value name="value">
<table>tbl_notice</table>
<sql>select title,content from tbl_notice order by id desc</sql>
<style></style>
</value>
</result>

 

需要jaxen-xx.xx.jar

你可能感兴趣的:(exception,xml,String,list,table,attributes)