import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLTest {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(new File("test/parameter.xml"));
// 先选取所有的parameter元素
List<?> parameterList = doc.selectNodes("//*[name()='parameter']/parent::*");
Iterator<?> parameterItr = parameterList.iterator();
while(parameterItr.hasNext()) {
Element parameterElement = (Element)parameterItr.next();
System.out.println("Current parameterElement XPath: " + parameterElement.getPath());
String parameterName = parameterElement.attributeValue("name"); // parameter name
String parameterClass = parameterElement.attributeValue("class"); // parameter class
System.out.println("parameterName = " + parameterName + "; parameterClass = " + parameterClass);
System.out.println("--------------------------------------------------------------------------");
}
// 再选取所有的field元素
// List<?> fieldList = doc.selectNodes("//*[name()='field']");
// Iterator<?> fieldItr = fieldList.iterator();
// while(fieldItr.hasNext()) {
// Element fieldElement = (Element)fieldItr.next();
// System.out.println("Current fieldElement XPath: " + fieldElement.getPath());
// String fieldName = fieldElement.attributeValue("name"); // field name
// String fieldClass = fieldElement.attributeValue("class"); // field class
// System.out.println("--------------------------------------------------------------------------");
// }
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ShipmentsReport" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="812" leftMargin="15" rightMargin="15" topMargin="10" bottomMargin="10">
<property name="net.sf.jasperreports.export.pdf.tagged" value="true"/>
<property name="net.sf.jasperreports.export.pdf.tag.language" value="EN-US"/>
<style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
<style name="Sans_Large" isDefault="false" style="Sans_Normal" fontSize="10"/>
<style name="Sans_Bold" isDefault="false" fontName="DejaVu Sans" fontSize="8" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
<parameter name="reportTitle" class="java.lang.String"/>
<subDataset name="Country_Orders">
<parameter name="Country" class="java.lang.String"/>
<queryString>
<![CDATA[SELECT ShippedDate, ShipRegion, ShipCity, Freight
FROM Orders
WHERE
ShipCountry = $P{Country} AND
ShippedDate IS NOT NULL]]>
</queryString>
<field name="ShippedDate" class="java.sql.Timestamp"/>
<field name="ShipRegion" class="java.lang.String"/>
<field name="ShipCity" class="java.lang.String"/>
<field name="Freight" class="java.lang.Float"/>
</subDataset>
<queryString>
<![CDATA[SELECT DISTINCT ShipCountry FROM Orders]]>
</queryString>
<field name="ShipCountry" class="java.lang.String"/>
</jasperReport>
List<?> parameterList = doc.selectNodes("//*[name()='parameter']/parent::*"); 获得所有的parmeter 的父结点
List<?> parameterList = doc.selectNodes("//*[name()='parameter']"); 获得所有的parmeter结点