呃~~~~~不是看过资料的吗?
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("XXX.xml"));
Iterator applications = document.selectNodes("//application ").iterator();然后迭代这个applications
用Element 和 Attribute获取属性Element application = (Element)applications .next();Attribute url= application .attribute("url");
...
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("XXX.xml"));
Iterator applications = document.selectNodes("//application ").iterator();然后迭代这个applications
用Element 和 Attribute获取属性Element application = (Element)applications .next();Attribute url= application .attribute("url");
...
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;public class XPathTest { protected String xpath = "*";
protected XMLWriter writer;
public static void main(String[] args) {
run(new XPathTest(), args);
} public XPathTest() {
} protected static void run(XPathTest demo, String[] args) {
try {
demo.run(args);
} catch (DocumentException e) {
System.out.println("Exception occurred: " + e);
Throwable nestedException = e.getNestedException();
if (nestedException != null) {
System.out.println("NestedException: " + nestedException);
nestedException.printStackTrace();
} else {
e.printStackTrace();
}
} catch (Throwable t) {
System.out.println("Exception occurred: " + t);
t.printStackTrace();
}
}
public void run(String[] args) throws Exception {
String xmlFile = "applications.xml";
xpath = "//applications/application";
writer = createXMLWriter(); Document document = parse(xmlFile);
process(document);
} protected XMLWriter createXMLWriter() throws Exception {
OutputFormat format = new OutputFormat();
return new XMLWriter(System.out, format);
}
protected Document parse(String xmlFile) throws Exception {
SAXReader reader = new SAXReader();
return reader.read(xmlFile);
}
protected void print(String text) {
System.out.print(text);
} protected void println(String text) {
System.out.println(text);
} protected void process(Document document) throws Exception {
println("Evaluating XPath: " + xpath);
List list = document.selectNodes(xpath); println("Found: " + list.size() + " node(s)");
println("Results:"); for (Iterator iter = list.iterator(); iter.hasNext();) {
Object object = iter.next();
writer.write(object);
writer.println();
}
writer.flush();
}
}
while(applications.hasNext())
{
Element application = applications.next();
Attribute appname = application.attribute("name");
System.out.println("application name :" + appname.getValue());
Element url = application.element("url");
String urlValue = url.getText();
System.out.println("URL :" + urlValue);
//DefaultText urlValue = (DefaultText) url.content().get(0);
//System.out.println("URL :" + urlValue.getText());}我之前写的方法复杂了~~To 5楼:
attribute是用来读取application的name的。